CATransition 是CAAnimation的子类,用了控制过渡动画。有一个type和subtype来标识变换效果。
-(void)Animation{
self.colorlayer = [CALayer layer];
self.colorlayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
self.colorlayer.backgroundColor = [UIColor blueColor].CGColor;
//add a custom action
CATransition *transition = [CATransition animation];
//样式
transition.type = kCATransitionPush;
//方向
transition.subtype = kCATransitionFromTop;
self.colorlayer.actions = @{@"backgroundColor": transition};
//add it to our view
[self.layerView.layer addSublayer:self.colorlayer];
}
- (IBAction)actionBtnclick:(id)sender {
[CATransaction begin];
//调整动画时长 隐式动画默认时长0.25
[CATransaction setAnimationDuration:0.5];
[CATransaction setCompletionBlock:^{
NSLog(@"动画结束回掉");
}];
CGFloat red = arc4random() / (CGFloat)INT_MAX;
CGFloat green = arc4random() / (CGFloat)INT_MAX;
CGFloat blue = arc4random() / (CGFloat)INT_MAX;
self.colorlayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
[CATransaction commit];
}
隐式动画禁用
实际应用:[UICollectionView reloadData] 会出现view/layer
闪烁
问题。是由于layer的隐式动画。
我在设置hidden属性时出发了闪烁
,故调用+ (void)setDisableActions:(BOOL)flag
取消隐式动画
[CATransaction begin];
[CATransaction setDisableActions:YES];
//layer 属性
for ( CAGradientLayer *gl in _layeArr) {
if (sender.tag == [_layeArr indexOfObject:gl]+666) {
gl.hidden = NO;
}else{
gl.hidden = YES;
}
}
[CATransaction commit];