路漫漫其修远兮,吾将上下而求索。
在苹果系统以及我们常用的著名的三方框架中,我们经常会遇到位运算,比如像系统中的UIView动画里面的位运算<<
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, // do not inherit any options or animation type
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
UIViewAnimationOptionPreferredFramesPerSecondDefault = 0 << 24,
UIViewAnimationOptionPreferredFramesPerSecond60 = 3 << 24,
UIViewAnimationOptionPreferredFramesPerSecond30 = 7 << 24,
} NS_ENUM_AVAILABLE_IOS(4_0);
亦或像下面的SDWebImage里面的位运算:
typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
SDWebImageDownloaderLowPriority = 1 << 0,
SDWebImageDownloaderProgressiveDownload = 1 << 1,
/**
* By default, request prevent the use of NSURLCache. With this flag, NSURLCache
* is used with default policies.
*/
SDWebImageDownloaderUseNSURLCache = 1 << 2,
/**
* Call completion block with nil image/imageData if the image was read from NSURLCache
* (to be combined with `SDWebImageDownloaderUseNSURLCache`).
*/
SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
/**
* In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
* extra time in background to let the request finish. If the background task expires the operation will be cancelled.
*/
SDWebImageDownloaderContinueInBackground = 1 << 4,
/**
* Handles cookies stored in NSHTTPCookieStore by setting
* NSMutableURLRequest.HTTPShouldHandleCookies = YES;
*/
SDWebImageDownloaderHandleCookies = 1 << 5,
/**
* Enable to allow untrusted SSL certificates.
* Useful for testing purposes. Use with caution in production.
*/
SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
/**
* Put the image in the high priority queue.
*/
SDWebImageDownloaderHighPriority = 1 << 7,
/**
* Scale down the image
*/
SDWebImageDownloaderScaleDownLargeImages = 1 << 8,
};
那么这种枚举有什么不一样的地方呢,其实这种枚举类型非常强大,也非常好用,下面通过一个小例子来说明:
// 第一种写法 -这是C的写法
typedef enum {
EnumDemoTypeTop,
EnumDemoType1Bottom,
}EnumDemoType1;
//第二种写法 -这是OC的写法
typedef NS_ENUM(NSInteger, EnumDemoType2) {
EnumDemoTypeLeft,
EnumDemoTypeRight,
};
//第三种写法
typedef NS_OPTIONS(NSInteger, EnumDemoActionType) {
EnumDemoActionTypeTop = 1<<0, //1*2(0) = 1 /1*二的零次方 = 1
EnumDemoActionType1Bottom = 1<<1, //1*2(1)=2
EnumDemoActionTypeLeft = 1<<2,//1*2(2)=4
EnumDemoActionTypeRight = 1<<3,//1*2(3)=8
};
使用位运算,就可以同时支持多个值:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self enumDemoType:EnumDemoActionTypeTop | EnumDemoActionType1Bottom | EnumDemoActionTypeLeft | EnumDemoActionTypeRight];
}
//按位与 & 1&1==1 1&0==0 0&0==1 只要有0则为0
//按位或 | 1|1==1 1|0==1 0|0=0 只要有1则为1
- (void)enumDemoType:(EnumDemoActionType)type {
NSLog(@"type:%ld",type);
if (type & EnumDemoActionTypeTop) {
NSLog(@"向上----%zd",type & EnumDemoActionTypeTop);
}
if (type & EnumDemoActionType1Bottom) {
NSLog(@"向下----%zd",type & EnumDemoActionType1Bottom);
}
if (type & EnumDemoActionTypeLeft) {
NSLog(@"向左----%zd",type & EnumDemoActionTypeLeft);
}
if (type & EnumDemoActionTypeRight) {
NSLog(@"向右----%zd",type & EnumDemoActionTypeRight);
}
}
网友评论