位与运算在iOS中的使用
来源:互联网 发布:图片无缝拼接软件 编辑:程序博客网 时间:2024/05/17 22:34
先看一段我们常见的代码
<span style="font-size:14px;">UIView *view = [[UIView alloc]init];view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;[self.view addSubview:view];</span>
很多初学者明白这句代码的意思是 保证与superView左边和右边的距离不变,顶部和底部的距离不变。
<span style="font-size:14px;">view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;</span>但是细看发现 autoresizingMask 的类型是 NSUInteger
<span style="font-size:14px;">typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) { UIViewAutoresizingNone = 0, UIViewAutoresizingFlexibleLeftMargin = 1 << 0, UIViewAutoresizingFlexibleWidth = 1 << 1, UIViewAutoresizingFlexibleRightMargin = 1 << 2, UIViewAutoresizingFlexibleTopMargin = 1 << 3, UIViewAutoresizingFlexibleHeight = 1 << 4, UIViewAutoresizingFlexibleBottomMargin = 1 << 5};</span>
<< 就是左移运算符 a << 4 只是把a的各二进位左移4位
UIViewAutoresizingFlexibleWidth = 1 << 1 相当于 0001 << 1 = 00010
UIViewAutoresizingFlexibleHeight = 1 << 4 相当于 0001 << 4 = 10000
再介绍一下 或运算 | 只要对应的二个二进位有一个为1时,结果位就为1
00001001
|
00000101
=
00001101
当我们写UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight 时,00010 |10000 = 10010. 实际就是
那我么如何知道,autoresizingMask 选中了 这个2个选项呢?
需要用到 与运算 &只有对应的二个二进位都为1时,结果位才是1
<span style="font-size:14px;">if(view.autoresizingMask & UIViewAutoresizingFlexibleWidth){ NSLog(@"UIViewAutoresizingFlexibleWidth 被选中了,需要保证与superView左边和右边的距离不变"); } if(view.autoresizingMask & UIViewAutoresizingFlexibleHeight){ NSLog(@"UIViewAutoresizingFlexibleHeight 被选中了,需要保证与superView顶部和底部的距离不变"); } if(view.autoresizingMask & UIViewAutoresizingFlexibleLeftMargin){ NSLog(@"UIViewAutoresizingFlexibleLeftMargin 被选中了,需要保证与superView右边的距离不变"); }</span>
若是运行上面的代码,则只会打印 line1 和 line2 ,不会打印line3
我么知道 autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight =00010 |10000 = 10010 。
1 view.autoresizingMask & UIViewAutoresizingFlexibleWidth = 10010 &00010 = 00010 = 2. 所以为真 line1执行
2view.autoresizingMask & UIViewAutoresizingFlexibleHeight = 10010 & 10000 = 10000 = 16 同样为真 line2 执行
3 UIViewAutoresizingFlexibleLeftMargin = 1 << 0 = 00001 << 0 = 00001
view.autoresizingMask & UIViewAutoresizingFlexibleLeftMargin = 100010 & 00001 = 00000 = 0 为假, 所以line3不会被执行。
所以,我么只需要稍微理解 3个运算符 (& | <<) 就能很好理解
<span style="font-size:14px;">view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;</span>
这样写的原因和好处了。
在实际应用中,我们也可以模仿这样一种写法。例如在图文混排时,我么通常需要在一段文字中,检测是否有网络地址URL, 是否有表情符号Emoji,是否有电话号码,,是否有邮箱等等。
<span style="font-size:14px;">typedef NS_OPTIONS(NSUInteger, FWDetectorTypes) { FWDetectorTypesNone = 0, FWDetectorTypesURL = 1 << 0, //1 FWDetectorTypesEmoji = 1 << 1, //2 FWDetectorTypesPhoneNumber = 1 << 2, //4 FWDetectorTypesEmail = 1 << 3, //8};</span>可以这样定义。
<span style="font-size:14px;">/** 需要识别的类型:默认识别表情 **/@property(nonatomic,assign)FWDetectorTypes types;</span>
如果我们只需要识别表情 types =FWDetectorTypesEmoji;
如果我们需要同时识别表情,电话,链接types = FWDetectorTypesEmoji |FWDetectorTypesURL|FWDetectorTypesPhoneNumber
在内容实现中,我么可以通过
types&FWDetectorTypesEmoji 来判断是否需要 执行识别表情的代码
types&FWDetectorTypesPhoneNumber 来判断 是否需要识别电话号码 等等。
其实上面所有的写法,本质就是 & | << 这三个符号的运算。根据运算后的结果是否大于0,来进行判断。
- 位与运算在iOS中的使用
- 位与运算符 在权限中的使用
- 位运算在Android中的使用场景
- iOS 位与运算
- 位运算在排序与搜索中的应用
- 位与运算在权限管理中的应用
- 位运算在排序与搜索中的应用(转)
- c#按位与运算在业务设计中的应用
- 位运算在算法编程中的使用技巧
- 位运算在算法编程中的使用技巧
- 位运算在算法题中的使用(C++)
- 位运算在Android Flag中的巧妙使用
- Cocoa中的位与位运算
- Cocoa中的位与位运算
- 位“异或”运算在变量交换中的应用及与位“或”的逻辑关系
- Java位运算在程序设计中的使用:位掩码(BitMask)
- Java位运算在程序设计中的使用:位掩码(BitMask)
- Java位运算在程序设计中的使用:位掩码(BitMask)
- 根文件系统的构建与分析(二)之构建动态库
- [精]Odoo 8.0深入浅出开发教程(五) Odoo开发模块
- redis安装
- ios 清空scrollview或view上的button
- 根文件系统的构建与分析(三)之根文件目录及最简/dev目录
- 位与运算在iOS中的使用
- java类对象进行equals比较的方法
- leetcode292 Nim Game
- C++中值传递、指针传递、引用传递
- Apache与Nginx比较(Nginx 高并发情况常用,必须学习)
- 树莓派下使用USB摄像头
- 根文件系统的构建与分析(四)之瑞士军刀busybox生成系统基本命令
- 关于socket编程中io流的总结
- Chipscope使用简介