1. 程式人生 > >自定義動畫改變背景顏色

自定義動畫改變背景顏色

@interface ViewController ()

 

@property (nonatomic, strong) CALayer *colorLayer;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    //create sublayer

 

    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 = kCATransitionFromLeft;

    

self.colorLayer.actions = @{@"backgroundColor": transition};

 

    //add to view

    [self.view.layer addSublayer:self.colorLayer];

}

 

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

{

    //randomize the layer background color

    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;

}

@end

7.3.jpeg