Reputation: 11
I'm trying to animate the sublayer of a UIView's layer with the following code:
CALayer *sublayer = [[CALayer alloc] init];
[sublayer setFrame:myRect]; // myRect is a portion of the view's frame rectangle
[self.layer addSublayer:sublayer];
and later in my drawRect: method:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
anim.duration = 1.0f;
anim.fromValue = [NSNumber numberWithFloat:1];
anim.toValue = [NSNumber numberWithFloat:.5];
anim.delegate = self;
[sublayer addAnimation:anim forKey:@"animateOpacity"];
I don't understand why my sublayer doesn't animate since the code [self.layer addAnimation:anim forKey:@"animateOpacity"];
does animate the view's main layer.
Thanks.
Upvotes: 1
Views: 2095
Reputation: 699
You can't animate the frame property. Animate the bounds or position instead.
Upvotes: 2
Reputation: 32316
Don't add CAAnimation
s during the drawRect:
method. CoreAnimation calls it when it needs to redraw the content of the view (which could be at any time). You should instead add the animation after the view has been added to its parent.
Upvotes: 3