自定义动画改变背景颜色

@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

猜你喜欢

转载自blog.csdn.net/majiakun1/article/details/82711788