Search code examples
cocoacore-animationquartz-graphics

Change animation time for properties of a CALayer


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?


Solution

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

    - (void)awakeFromNib {
        yourLayer.delegate = self;
        yourLayer.name = @"yourLayer";
    }  
    - (id <CAAction>)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
    }