开篇用下我之前的文章里的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)
这样的判断语句,这里用到的运算符是按位与。假如我现在的_layerProperty
为 layerPropertyPosition | 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];
}
网友评论