CATransition 是CAAnimation的子类,用了控制过渡动画。有一个type和subtype来标识变换效果。
CATransition1
CATransition2
-(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];
}
网友评论