美文网首页
iOS 使用位移枚举来完成多选值

iOS 使用位移枚举来完成多选值

作者: Maj_sunshine | 来源:发表于2018-05-29 22:29 被阅读36次

开篇用下我之前的文章里的NS_OPTIONS枚举来介绍。

typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
    UIControlEventTouchDown                                         = 1 <<  0,      // on all touch downs
    UIControlEventTouchDownRepeat                                   = 1 <<  1,      // on multiple touchdowns (tap count > 1)
    UIControlEventTouchDragInside                                   = 1 <<  2,
    UIControlEventTouchDragOutside                                  = 1 <<  3,
    UIControlEventTouchDragEnter                                    = 1 <<  4,
    UIControlEventTouchDragExit                                     = 1 <<  5,
    UIControlEventTouchUpInside                                     = 1 <<  6,
    UIControlEventTouchUpOutside                                    = 1 <<  7,
    UIControlEventTouchCancel                                       = 1 <<  8,
}

这是一个NS_OPTIONS类型的枚举,从上到下的值分别是二级制的

0000 0001 , // 1 * 2^0 = 1
0000 0010, // 1 * 2^1 = 2
0000 0100, // 1 * 2^2 = 4
0000 1000, // 1 * 2^3 = 8
0001 0000, // 1 * 2^4 = 16
0010 0000, // 1 * 2^5 = 32
0100 0000, // 1 * 2^6 = 64
1000 0000, // 1 * 2^7 = 128
0001 0000 0000, //1 * 2^8 = 256

如果我们想要事件在点下后在控件内离开界面我们会用UIControlEventTouchUpInside,我们又想要控件在内部拖动的时候也响应就变成UIControlEventTouchDragEnter| UIControlEventTouchUpInside,之前的文章已经说过位移枚举使用枚举值累加来获取唯一的枚举值,那么这里就是0101 000 ,64 + 16 = 80。

利用这个我们可以用位移枚举来做一个多选
  • 创建一个NS_OPTIONS枚举
typedef NS_OPTIONS(NSInteger, LayerProperty) {
    layerPropertyNone = 0,
    layerPropertyPath = 1 << 0,  // 路径
    layerPropertyPosition = 1 << 1,  // 位置
    layerPropertyAnchorPoint = 1 << 2,  // 锚点
    layerPropertyBackgroundColor = 1 << 3,  // 背景颜色
    layerPropertyBorderColor = 1 << 4,  // 边框颜色
    layerPropertyBorderWidth = 1 << 5,  // 边框宽度
    layerPropertyBounds = 1 << 6,  // 位置大小
    layerPropertyContents = 1 << 7,  // 内容
    layerPropertyContentsRect = 1 << 8,  // 内容大小 
    layerPropertyCornerRadius = 1 << 9,  // 半径
    layerPropertyDoubleSided = 1 << 10,  // 图层背景显示
    layerPropertyFrame = 1 << 11,  // 图层位置大小
    layerPropertyHidden = 1 << 12,  // 隐藏
    layerPropertyMask = 1 << 13,  // 遮罩
    layerPropertyMasksToBounds = 1 << 14,  // 裁剪
    layerPropertyOpacity = 1 << 15,  // 透明度
    layerPropertyShadowColor = 1 << 16,  // 阴影颜色
    layerPropertyShadowOffset = 1 << 17,  // 阴影偏移
    layerPropertyShadowOpacity = 1 << 18,  // 阴影透明度
    layerPropertyShadowPath = 1 << 19,  // 阴影路径
    layerPropertyShadowRadius = 1 << 20,  // 阴影半径
    layerPropertyFillColor = 1 << 21,  // 填充颜色
    layerPropertyFillRule = 1 << 22,  // 填充规则
    layerPropertyStrokeColor = 1 << 23,  // 线的颜色
    layerPropertyStrokeStart = 1 << 24,  // 绘制开始位置
    layerPropertyStrokeEnd = 1 << 25,  // 绘制结束位置
    layerPropertyLineWidth = 1 << 26,  // 显得宽度
    layerPropertyMiterLimit = 1 << 27,  // 拐角内角和外角的距离
    layerPropertyLineCap = 1 << 28,  // 拐角样式
    layerPropertyLineJoin = 1 << 29,  // 连接点样式
    layerPropertyLineDashPhase = 1 << 30,  // 虚线偏移值
    layerPropertyLineDashPattern = 1 << 31,  // 虚实线宽度数组
};
  • 多选时,用加法来进行枚举的叠加,减法来进行枚举的删除
 // cell选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     // 判断颜色是否相同
    if (CGColorEqualToColor(cell.textLabel.textColor.CGColor, kGetColor(102, 102, 102, 1).CGColor)) {
        if (indexPath.row > 1) {
             // 位移枚举做加法,这里加的是底数为2的指数函数,如果 indexPath.row = 3 ,则_layerProperty = _layerProperty + 4
            _layerProperty += pow(2, indexPath.row - 1);
        }
        cell.textLabel.textColor = [UIColor greenColor];
    } else {
        if (indexPath.row > 1) {
             // 位移枚举做减法,这里减的是底数为2的指数函数,indexPath.row = 3 ,则_layerProperty = _layerProperty - 4
            _layerProperty -= pow(2, indexPath.row - 1);
        }
        cell.textLabel.textColor = kGetColor(102, 102, 102, 1);
    }
    
     // set赋值
    _layerView.layerProperty = _layerProperty;
}
  • 判断多选
    在SDWebImage源码中,我们经常看到类似
(options & SDWebImageForceTransition || cacheType == SDImageCacheTypeNone)

这样的判断语句,这里用到的运算符是按位与。假如我现在的_layerPropertylayerPropertyPosition | layerPropertyBackgroundColor

那么现在的_layerProperty就为1010 ,判断时(_layerProperty & layerPropertyBackgroundColor)表示为1010 和 1000按位与结果为1000,我们理解为枚举值里有layerPropertyBackgroundColor这个枚举。

判断(layerProperty & layerPropertyAnchorPoint)表示1010和0100按位与,结果为0000,我们理解为当前枚举值里没有layerPropertyAnchorPoint这个枚举

  • 判断使用枚举值
- (void)setLayerProperty:(LayerProperty)layerProperty {
     // 返回原样
    if (layerProperty == 0 || (layerProperty & layerPropertyPath)) {
        [self backOrigin];
        return;
    }
     // 动画延迟时间
    _beginTime = 0;
    _duration = 0;
    CAAnimationGroup *group = [CAAnimationGroup animation];
    NSMutableArray *animationArray = [NSMutableArray array];
    
     // 改变Position
    if (layerProperty & layerPropertyPosition) {
        CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
        positionAnimation.duration = 1;
        [self addTime:positionAnimation];
        [animationArray addObject:positionAnimation];
    }
    
     // 改变锚点
    if (layerProperty & layerPropertyAnchorPoint) {
        _shapeLayer.anchorPoint = CGPointMake(0, 0);
        _aniLayer.anchorPoint = CGPointMake(0, 0);
        CABasicAnimation *anchorAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
        anchorAnimation.toValue = @(M_PI * 2);
        anchorAnimation.beginTime = _beginTime;
        anchorAnimation.duration = 2;
        [self addTime:anchorAnimation];
        [animationArray addObject:anchorAnimation];
    }
    
    // 边框颜色颜色
    if (layerProperty & layerPropertyBorderColor) {
        CABasicAnimation *borderColorAnimation = [CABasicAnimation animationWithKeyPath:@"borderColor"];
        borderColorAnimation.fromValue = (__bridge id _Nullable)([UIColor purpleColor].CGColor);
        borderColorAnimation.toValue = (__bridge id _Nullable)([UIColor greenColor].CGColor);
        borderColorAnimation.duration = 2;
        borderColorAnimation.beginTime = _beginTime;
        [self addTime:borderColorAnimation];
        [animationArray addObject:borderColorAnimation];
    }
    
     // 背景颜色
    if (layerProperty & layerPropertyBackgroundColor) {
        CABasicAnimation *bgColorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
        bgColorAnimation.fromValue = (__bridge id _Nullable)([UIColor purpleColor].CGColor);
        bgColorAnimation.toValue = (__bridge id _Nullable)([UIColor greenColor].CGColor);
        bgColorAnimation.duration = 2;
        bgColorAnimation.beginTime = _beginTime;
        [self addTime:bgColorAnimation];
        [animationArray addObject:bgColorAnimation];
    }
   
    group.animations = animationArray;
    group.duration = _duration;
    group.fillMode = kCAFillModeForwards;
    group.removedOnCompletion = NO;
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [_shapeLayer addAnimation:group forKey:@"groupAnimation"];
    [_aniLayer addAnimation:group forKey:@"aniLayerAnimation"];
    [self layoutIfNeeded];
}

相关文章

  • iOS 使用位移枚举来完成多选值

    开篇用下我之前的文章里的NS_OPTIONS枚举来介绍。 这是一个NS_OPTIONS类型的枚举,从上到下的值分别...

  • iOS位移枚举的理解

    整数型枚举就是一个萝卜一个坑,一种情况对应一个枚举,但是是单选, 而位移枚举则是可以多选,在使用位移枚举的情况下 ...

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

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

  • iOS枚举(位移枚举)

    什么是枚举 在程序设计语言中,一般用一个数值来代表某一状态,这种处理方法不直观,易读性差。如果能在程序中用自然语言...

  • iOS枚举

    在iOS中定义枚举可以帮我们减轻不少工作枚举定义有两种一种是数值 一种是按照位移 位移的用处在于可以组合使用,比如...

  • iOS位移枚举

    位移枚举 目录 问题引入 问题解析 概念讲解 问题解决办法(位移枚举) 优化方法 问题引入 假定有一项考试,考试内...

  • iOS 位移枚举

    待完成 每一天坚持学习

  • iOS开发 枚举类型NS_ENUM和NS_OPTIONS的区别

    NS_ENUM通用枚举值 NS_OPTIONS位移相关操作的枚举值 通常情况下我们用的枚举是NS_ENUM 当一个...

  • Rust 枚举

    枚举 枚举,也被称作enums。枚举允许你通过列举可能的值来定义一个类型。 定义枚举 枚举值 可以使用任一成员来调...

  • 为什么NS_OPTIONS要使用<<左移,左移这种写法和 NS_

    iOS本身定义的枚举里面经常会使用左移(<<)来定义枚举的值,一开始我还不懂,看了下面的原理就明白了,文章出处下面...

网友评论

      本文标题:iOS 使用位移枚举来完成多选值

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