CALayer的隐式动画
来源:互联网 发布:hosts网络源 编辑:程序博客网 时间:2024/05/28 05:16
何为隐式动画
隐式动画就是直接改变layer的一些属性时,并没有作动画处理,layer会附带有动画效果,而不是直接瞬间变化,这个动画时间默认是0.25秒。
layer的哪些属性有隐式动画
区分属性是不是具有隐式动画,要看属性定义有没有“Animatable”字样。比如
/* The background color of the layer. Default value is nil. Colors * created from tiled patterns are supported. Animatable. */@property(nullable) CGColorRef backgroundColor;
backgroundColor属性的定义最后标明了Animatable,说明修改它的值会有默认动画效果。
怎样修改隐式动画
一、通过CATransaction
在页面上放了一个layer,通过点击button修改它的颜色
@property (nonatomic,strong) UIView *backView;@property (nonatomic,strong) CALayer *colorLayer;@property (nonatomic,strong) UIButton *changeButton;- (void)viewDidLoad { [super viewDidLoad]; CGFloat kScreenWidth = [UIScreen mainScreen].bounds.size.width; _backView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth/2-100, 50, 200, 200)]; _backView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_backView]; _colorLayer = [CALayer layer]; _colorLayer.frame = CGRectMake(50, 50, 100, 100); _colorLayer.backgroundColor = [UIColor redColor].CGColor; _colorLayer.borderWidth = 0.5; _colorLayer.borderColor = [UIColor blackColor].CGColor; _colorLayer.delegate = self; [_backView.layer addSublayer:_colorLayer]; _changeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_changeButton addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside]; _changeButton.frame = CGRectMake(50, 160, 100, 20); [_changeButton setTitle:@"Change Color" forState:UIControlStateNormal]; [_changeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _changeButton.titleLabel.font = [UIFont systemFontOfSize:10]; _changeButton.titleLabel.textColor = [UIColor blackColor]; _changeButton.layer.cornerRadius = 5; _changeButton.layer.borderWidth = 0.5; _changeButton.layer.borderColor = [UIColor blackColor].CGColor; _changeButton.backgroundColor = [UIColor whiteColor]; [_backView addSubview:_changeButton];}- (void)changeColor{ _colorLayer.backgroundColor = [UIColor blueColor].CGColor;]}
为了修改隐式动画,我们对changeColor作修改
- (void)changeColor{ [CATransaction begin]; [CATransaction setAnimationDuration:2]; _colorLayer.backgroundColor = [UIColor blueColor].CGColor; [CATransaction commit];}
这样点击改变颜色的隐式动画的时间就被修改为2秒。
CATransaction还提供了禁用隐式动画和设置完成动作等方法。
二、通过layer的actions属性
CATransition *transition = [CATransition animation]; transition.type = @"push"; transition.subtype = @"fromLeft"; _colorLayer.actions = @{@"backgroundColor":transition};
这样在改变颜色的过程中就会附带推进的动画效果。
三、通过手动实现-actionForLayer:(CALayer )layer forKey:(NSString )event方法
_colorLayer.delegate = self;- (nullable id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event{ CABasicAnimation *animation = [CABasicAnimation animation]; animation.keyPath = @"backgroundColor"; animation.toValue = (__bridge id)[UIColor blueColor].CGColor; return animation;}
UIView中的layer是没有隐式动画的
UIView关联的layer的隐式动画是被禁用的,所以如果想要通过改变view.layer的属性来实现动画是行不通的,只能通过UIView动画方法或者显式的添加动画来实现。
0 0
- CALayer的隐式动画
- CALayer 的隐式动画
- CALayer简介 (七)CALayer的隐式动画属性
- CALayer的隐式动画实例 - 钟表
- CALayer的隐式动画和显式动画
- iOS CALayer 隐式动画
- iOS CALayer隐式动画
- XMG CALayer隐式动画
- CALayer关闭隐式动画
- ios-day18-04(图层CALayer的隐式动画)
- 三 iOS之 CALayer的隐式动画
- 动画—CALayer的初步学习一:CALayer属性—隐式动画(学会从博客上自学)
- CALayer的动画创建
- CALayer的动画属性
- CALayer常用属性,隐式动画
- CALayer介绍和隐式动画
- 隐式动画 - CALayer - 找规律游戏
- iOS核心动画之CALayer-隐式动画
- 自定义NSOperation进行多线程之间的通信(通知、代理、block)
- 学习NodeJS必经之路——Hello World
- 4. 【创建和销毁对象】通过私有化构造方法强化不可实例化的能力
- 22. Generate Parentheses
- 个人学习c++的真实经验
- CALayer的隐式动画
- 云计算初识
- 蓝懿ios技术交流和心得分享16.1.6
- linux串口链接打印软件-putty介绍
- php的学习经验
- Kinect桥接Matlab代码简介KinectBridge With Matlab
- 将一个数组中的值按逆序重新存放并输出
- 周易六十四卦——讼卦
- RESTful_URI资源