一 使用转场动画CATransition实现, 其中type 需要使用kCATransitionFade
CATransition *transition = [[CATransition alloc] init];
transition.duration = 3;
transition.fillMode = kCAFillModeForwards;
transition.type = kCATransitionFade;
[self.fadeButton.layer addAnimation:transition forKey:@"animation"];
self.fadeButton.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
二 使用UIView 的 transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion接口实现,其中option需要使用 UIViewAnimationOptionTransitionCrossDissolve枚举
[UIView transitionWithView:self.fadeButton duration:3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.fadeButton.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
} completion:^(BOOL finished) {
}];