简体   繁体   中英

iphone : animating a CALayer sublayer

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.

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.

You can't animate the frame property. Animate the bounds or position instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM