Change animation time for properties of a CALayer

前端 未结 3 515
遇见更好的自我
遇见更好的自我 2021-02-04 16:27

I have a CALayer to animate a change in its image contents. Now, how can I change how long it takes for this animation to take place?

3条回答
  •  失恋的感觉
    2021-02-04 17:16

    It's more or less simple. You have an ivar CALayer *yourLayer. Then you set the delegate and implement the delegate method -(id)actionForLayer:forKey:

    - (void)awakeFromNib {
        yourLayer.delegate = self;
        yourLayer.name = @"yourLayer";
    }  
    - (id )actionForLayer:(CALayer *)layer forKey:(NSString *)event {
        if([layer.name isEqualToString yourLayer.name]) { // Check for right layer
    
            CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:event]; // Default Animation for 'event'
            ani.duration = .5; // Your custom animation duration
            return ani;
    
        } else return nil; // Default Animation
    }
    

提交回复
热议问题