CABasicAnimation跳转页面返回动画停止处理

原因

CABasicAnimation动画在界面跳转后会自动停止并移除动画

处理方式

设置CABasicAnimation的delegate,在停止回调中在一次添加动画即可

代码如下,写了一个类别如下

也可以使用- (void)rotationPro360方法就不用处理停止的问题
UIView+HqView.h

//
//  UIView+HqView.h
//  GlobalPay
//
//  Created by hqmac on 2019/2/14.
//  Copyright © 2019 solar. All rights reserved.
//

#import 

NS_ASSUME_NONNULL_BEGIN

@interface UIView (HqView)

- (void)rotation360;
//360度旋转pro版
- (void)rotationPro360;

@end

NS_ASSUME_NONNULL_END

import "UIView+HqView.h"

- (void)rotation360{
    CABasicAnimation *animation =  [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //默认是顺时针效果,若将fromValue和toValue的值互换,则为逆时针效果
    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue =  [NSNumber numberWithFloat: M_PI *2];
    animation.duration  = 3;
    animation.autoreverses = NO;
    animation.delegate = self   ;//设置代理
    animation.fillMode =kCAFillModeForwards;
    animation.cumulative = YES;
    animation.repeatCount = CGFLOAT_MAX; //如果这里想设置成一直自旋转,可以设置为MAXFLOAT,否则设置具体的数值则代表执行多少次
    [self.layer addAnimation:animation forKey:@"transform.rotation.z"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
//    NSLog(@"animationDidStop == ");
//重新添加动画
    [self.layer addAnimation:anim forKey:@"transform.rotation.z"];
}
//360度旋转pro版本
- (void)rotationPro360{
    // 一秒钟旋转几圈
    CGFloat circleByOneSecond = 3.0f;
    // 执行动画
    [UIView animateWithDuration:1.f / circleByOneSecond
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         self.transform = CGAffineTransformRotate(self.transform, M_PI_2);
                     }
                     completion:^(BOOL finished){
                         [self rotation360];
                     }];
}

你可能感兴趣的:(CABasicAnimation跳转页面返回动画停止处理)