位移枚举
2016-08-04 本文已影响16人
Z了个L
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
//经典C语言风格的枚举
typedef enum : NSUInteger {
XMGTypeTop,
XMGTypeBottom,
} XMGType;
//OC风格的枚举
typedef NS_ENUM(NSUInteger,XMGDemoType) {
XMGDemoTypeTop,
XMGDemoTypeBottom,
};
//位移枚举
//如果发现位移枚举的第一个枚举值!=0,那么你可以默认传0,表示性能最高
typedef NS_OPTIONS(NSUInteger, XMGActionType) {
XMGActionTypeTop = 1 << 0, //1
XMGActionTypeBottom = 1 << 1,
XMGActionTypeLeft = 1 << 2,
XMGActionTypeRight = 1 << 3,
};
@interface ViewController ()
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self demo:XMGActionTypeTop | XMGActionTypeBottom | XMGActionTypeLeft | XMGActionTypeRight];
}
//传多个参数 3个
//| 0|1 = 1 0|0 = 0 1|1 = 1 只要有1那么结果就是1
//& 0&1 = 0 0&0 = 0 1&1 = 1 只要有0那么结果就是0
-(void)demo:(XMGActionType)type
{
NSLog(@"%zd",type);
if (type & XMGActionTypeTop) {
NSLog(@"向上---%zd",type & XMGActionTypeTop);
}
if (type & XMGActionTypeBottom) {
NSLog(@"向下---%zd",type & XMGActionTypeBottom);
}
if (type & XMGActionTypeRight) {
NSLog(@"向右---%zd",type & XMGActionTypeRight);
}
if (type & XMGActionTypeLeft) {
NSLog(@"向左---%zd",type & XMGActionTypeLeft);
}
}
@end