凡是涉及枚举传值时,不了解的就直接传入0 因为枚举有个约定,传入值若为0则不进行任何有关操作。如果要传递多个值则多个枚举值之间用或 | 连接 作为参数传递即可
新建工程,代码如下:
//
// ViewController.m
// 位移枚举详解
//
// Created by apple on 15/10/25.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
typedef enum {
CZActionTypeTop = 1<< 0,
CZActionTypeBottom = 1 << 1,
CZActionTypeLeft = 1 << 2,
CZActionTypeRight = 1 <<3,
}CZActionType;
@interface ViewController ()
@end
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self test:0]; // 注意:枚举有个约定 如果传值为0 则不做任何操作
// [self test:CZActionTypeTop];
// [self test:CZActionTypeBottom];
// [self test:CZActionTypeTop|CZActionTypeBottom | CZActionTypeLeft | CZActionTypeRight ];
}
-(void)test:(CZActionType) type
{
NSLog(@"%d", type);
if (type & CZActionTypeTop) {
NSLog(@"上");
}
if (type & CZActionTypeBottom) {
NSLog(@"下");
}
if (type & CZActionTypeLeft) {
NSLog(@"左");
}
if (type & CZActionTypeRight) {
NSLog(@"右");
}
}
/**
注意:
按位与 '&' 操作规则,只要有0便为0
按位或 '|' 操作规则,只要有1便为1
*/
@end
运行结果依次如下所示:
原理剖析如下: