OC 位移枚举

🌃
#import "NewViewController.h"
typedef NS_OPTIONS(NSUInteger, LKActionType) {
    LKActionTypeTop  =    1 << 0, // 1  左移0位
    LKActionTypeBottom  =    1 << 1, // 2  左移1位
    LKActionTypeLeft  =    1 << 2, // 4 左移2位
    LKActionTypeRight  =    1 << 3, // 8 左移3位
};
@interface ViewController ()
@end

@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    [self actionType:LKActionTypeBottom | LKActionTypeRight];
    NSLog(@"----------------------------------------分割线");
    [self actionType:LKActionTypeTop | LKActionTypeLeft | LKActionTypeRight];
}

// 位移枚举在传入参数时,是使用 或运算 的方式,
/*
先或后与比对出结果
以第一个举例:
LKActionTypeBottom 左移1位 0010 
LKActionTypeRight     左移3位 1000 
1.或操作为 1010 为10
2.然后在与操作匹配
1010 与 0010 为0010   2
1010 与 1000 为 1000  8

 
 */
//传多个参数 3个
//| (或运算符)   0|1 = 1 0|0 = 0 1|1 = 1  只要有1那么结果就是1
//& (与运算符)   0&1 = 0 0&0 = 0 1&1 = 1  只要有0那么结果就是0
-(void) actionType:(LKActionType)type
{
    NSLog(@"%zd",type);
    
    if (type &  LKActionTypeTop) {
        NSLog(@"top---%zd",type & LKActionTypeTop);
    }
//    1010;
//    0010;
    
    if (type & LKActionTypeBottom) {
        NSLog(@"bottom---%zd",type &  LKActionTypeBottom);
    }
    
    if (type & LKActionTypeRight) {
        NSLog(@"right---%zd",type &  LKActionTypeRight);
    }
    
    if (type &  LKActionTypeLeft) {
        NSLog(@"left---%zd",type &  LKActionTypeLeft);
    }
    
    
}

/*
 控制台输出:
10
bottom---2
right---8
 
----------------------------------------分割线
 
13
top---1
right---8
向左---4
 */
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。