CoreAnimation导致内存泄漏

案例:

执行关键帧动画如下:

    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
}

拓展:

类似也有UIWebViewdelegate, 需要手动释放.

UIWebView delegate

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,723评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,543评论 5 13
  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 8,404评论 1 23
  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 8,204评论 0 21
  • 先看看CAAnimation动画的继承结构 CAAnimation{ CAPropertyAnimation { ...
    时间不会倒着走阅读 5,645评论 0 1

友情链接更多精彩内容