Вопрос

I have looked at every other answer on here (and across the web) to a situation like mine and have yet to remedy the issue. Essentially what is occurring is I am animating an image-containing CALayer across a quadratic bezier curve. The animation works perfectly, but I really need to implement post-animation behavior, and every attempt I have made at doing so has failed. Here is my code:

UIBezierPath *path1 = [UIBezierPath bezierPath];
[path1 moveToPoint:P(67, 301)];
[path1 addQuadCurveToPoint:P(254, 301) controlPoint:P(160, 93)];

CALayer *ball1 = [CALayer layer];
ball1.bounds = CGRectMake(67, 301, 16.0, 16.0);
ball1.position = P(67, 301);
ball1.contents = (id)([UIImage imageNamed:@"turqouiseBallImage.png"].CGImage);
[self.view.layer addSublayer:ball1];

CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation1.path = path1.CGPath;
animation1.rotationMode = kCAAnimationRotateAuto;
animation1.repeatCount = 2.0;
animation1.duration = 5.0;
[ball1 addAnimation:animation1 forKey:@"ball1"];


[animation1 setDelegate:self];

I'm trying to set the delegate of the animation to self (my primary view controller; this is all in viewDidLoad), whose code implements the animationDidStop method:

(interface) (only showing relevant code)
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag;

(implementation)
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag; {
NSLog(@"animation did stop"); 
}

I do not see what I am doing that is causing the method not to be called. From my understanding (I am rather new to iOS programming, granted), the CAKeyFrameAnimation animation1 automatically sends the animationDidStop method to its designated delegate when it ceases to animate, which is precisely why I can't identify an issue.

Every other solution or alternative that I have seen has either not worked or suggested using block animation, for which I could not find a way to animate over a bezier curve.

Это было полезно?

Решение

Set the animation's delegate before calling -[CALayer addAnimation:forKey:].

Note the comment in CALayer.h:

The animation is copied before being added to the layer, so any
subsequent modifications to anim will have no affect unless it is added to another layer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top