美文网首页
iOS-位移枚举简单介绍

iOS-位移枚举简单介绍

作者: 翀鹰精灵 | 来源:发表于2019-03-13 15:22 被阅读0次

路漫漫其修远兮,吾将上下而求索。
在苹果系统以及我们常用的著名的三方框架中,我们经常会遇到位运算,比如像系统中的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);
    }
}


充分的用好枚举,可以增强代码的可读性与健壮性,让代码更加的规范。

相关文章

  • iOS-位移枚举简单介绍

    路漫漫其修远兮,吾将上下而求索。在苹果系统以及我们常用的著名的三方框架中,我们经常会遇到位运算,比如像系统中的UI...

  • 位移枚举简单介绍

    枚举的三种实现方式 第一种枚举 第二种枚举定义类型 ** 第三种枚举** 位移枚举在这感谢下原文作者[天狐博客]|...

  • Swift&OC位移枚举区别

    1、Swift 位移枚举写法 2、ObjC位移枚举写法 相比之下ObjC更加简单明了。

  • 位移枚举

    位移枚举和普通枚举的区别位移枚举可以传递多个参数,普通的枚举只能传递单个参数 举个?在SDWebimage里有的地...

  • iOS枚举——位移枚举的简单使用

    今天和大家一起来学习一下iOS位移枚举的简单使用,有疏忽的地方,还望各位不吝赐教。 一、枚举的作用 在代码中使用枚...

  • 位移枚举

    位移枚举 C语言枚举定义 苹果创建枚举的定义方式

  • 位移枚举

    枚举的其中一种方式:位移枚举,直接上代码,看完基本就懂了 写一个方法: 在viewDidLoad方法中调用本方法:

  • 位移枚举

    位移枚举 一. OC中常见的三种枚举 C语言枚举 // C语言枚举 typedef enum : NSUInteg...

  • 位移枚举

  • 位移枚举

    使用场景:同一次操作有多个选项,可以同时多选同时禁用摘取一段定义好的iOS代码 写一个方法,需要选择两种特性来实现...

网友评论

      本文标题:iOS-位移枚举简单介绍

      本文链接:https://www.haomeiwen.com/subject/hcqtmqtx.html