案例:
执行关键帧动画如下:
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
anim.keyPath = @"position";
CGPoint controlPoint = CGPointMake(startPoint.x+50, self.endPoint.y+50);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
[path addQuadCurveToPoint:self.endPoint controlPoint:controlPoint];
anim.path = path.CGPath;
anim.duration = [self durationWithStartPoint:startPoint controlPoint:controlPoint endPoint:self.endPoint];
anim.delegate = self;
anim.fillMode = kCAFillModeForwards;
anim.removedOnCompletion = NO;
[self.playIcon.layer addAnimation:anim forKey:@"animationTest"];
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
//do something
}
self对象无法释放.
分析:
查询CAAnimation的delegate:
/* The delegate of the animation. This object is retained for the
* lifetime of the animation object. Defaults to nil. See below for the
* supported delegate methods. */
@property(nullable, strong) id <CAAnimationDelegate> delegate;
在动画的生命周期delegate都会被强引用.
引用关系
一般情况执行完毕动画后,会自动释放.
/* When true, the animation is removed from the render tree once its
* active duration has passed. Defaults to YES. */
@property(getter=isRemovedOnCompletion) BOOL removedOnCompletion;
但当设置为NO时,anim.removedOnCompletion = NO;
的时候, 需要我们手动释放.
解决:
动画结束时,手动移除动画.
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
[self.playIcon.layer removeAnimationForKey:@"animationTest"];
//do something
}
拓展:
类似也有UIWebView
的delegate
, 需要手动释放.