@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

自定义动画改变背景颜色

相关文章:

  • 2021-07-01
  • 2021-09-04
  • 2021-06-15
  • 2021-12-19
  • 2021-08-04
  • 2022-02-07
  • 2021-05-31
猜你喜欢
  • 2022-12-23
  • 2021-05-18
  • 2021-07-06
  • 2021-11-21
  • 2021-06-08
  • 2021-05-20
  • 2022-01-20
相关资源
相似解决方案