使用CAAnimationGroup组合关键帧动画,设置delegate,造成VC无法释放问题
今天使用CoreAnimation写了个Demo,写的时候使用到了CAAnimationGroup和CAKeyframeAnimation,由于需要知道该组合动画的结束时间,所以写了一句
_group.delegate = self;
结果造成了当前的VC无法释放,最后找到了解决方法.
原来这个协议方法
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag的调用并不代表layer的动画真正的界面,只是我们看到的结束而已,为了使VC得到释放,我们需要在这个协议方法里再将delegate置为nil
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ NSLog(@"%s",__func__); _group.delegate = nil; }最后还需要在ViewWillDisappear里面移除层动画,
- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self.animationView.layer removeAllAnimations]; }大功告成,这是我们返回主界面,VC就会得到释放.
下面是我实现的简单效果图:
下面是完整代码:
#import "PlayAnimationViewController.h" @interface PlayAnimationViewController (){ CAAnimationGroup *_group; } @property (weak, nonatomic) IBOutlet UIView *animationView; @property (weak, nonatomic) IBOutlet UIView *centerPositionView; @end @implementation PlayAnimationViewController - (void)viewDidLoad { [super viewDidLoad]; self.centerPositionView.layer.masksToBounds = YES;//中心绿色小圆 self.centerPositionView.layer.cornerRadius = 5.0f; self.animationView.layer.masksToBounds = YES;//灰色背景圆 self.animationView.layer.cornerRadius = 45.0f; [self animationWithLayer:self.animationView.layer]; // Do any additional setup after loading the view from its nib. } - (void)animationWithLayer:(CALayer *)layer{ CAKeyframeAnimation * scaleAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; scaleAnim.values = @[[NSValue valueWithCATransform3D:CATransform3DIdentity], [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.6, 0.6, 1.0)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.6, 0.6, 1.0)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1.0)] ]; scaleAnim.keyTimes = @[@(0),@(0.3),@(0.7),@(1)]; scaleAnim.autoreverses = NO; scaleAnim.fillMode = kCAFillModeForwards; scaleAnim.removedOnCompletion = NO; CAKeyframeAnimation * posAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"]; posAnim.values = @[[NSValue valueWithCGPoint:layer.position], [NSValue valueWithCGPoint:layer.position], [NSValue valueWithCGPoint:CGPointMake(layer.position.x+200, 400)], [NSValue valueWithCGPoint:CGPointMake(layer.position.x+200, 400)] ]; posAnim.keyTimes = @[@(0),@(0.3),@(0.7),@(1)]; posAnim.autoreverses = NO; posAnim.fillMode = kCAFillModeForwards; posAnim.removedOnCompletion = NO; _group = [CAAnimationGroup animation]; _group.animations = @[scaleAnim,posAnim]; _group.duration = 5.0f; _group.autoreverses = NO; _group.fillMode = kCAFillModeForwards; _group.removedOnCompletion = NO; _group.delegate = self; [layer addAnimation:_group forKey:nil]; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [self.animationView.layer removeAllAnimations]; } - (void)animationDidStart:(CAAnimation *)anim{ NSLog(@"%s",__func__); } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ NSLog(@"%s",__func__); _group.delegate = nil; } @end
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了