iOS刷新控件的旋转效果

思想:用一个贝塞尔曲线画一个圆,然后用一个CAShapLayer对象的路径等于贝塞尔曲线的路径。将这个layer对象做为当前视图的子layer,然后在做旋转动画。

旋转动画一定要加到当前的view.layer上


总的来说就是旋转这个view而不是旋转这个CAShapLayer对象


#import "MyView.h"

#define SIZE_RADIUS_WIDTH   30
@interface MyView ()

@property (nonatomic , strong) UIView *toast;

@property (nonatomic, strong)UIView *rotateView;

@property (nonatomic, strong)CAShapeLayer *rotateLayer;


@end

@implementation MyView


- (void)drawRect:(CGRect)rect {
    
//    _toast = [[UIView alloc] initWithFrame:CGRectMake((rect.size.width - toastWidth) / 2, (rect.size.height - toastWidth) / 2 , toastWidth, toastWidth)];
//    _toast.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.8];
//    _toast.layer.cornerRadius = 10;
//
//    [self addSubview:_toast];

//    
//    _rotateLayer = [CAShapeLayer layer];
//    _rotateView = [[UIView alloc] initWithFrame:CGRectMake((_toast.frame.size.width - 2 * SIZE_RADIUS_WIDTH) / 2, (_toast.frame.size.height - 2 * SIZE_RADIUS_WIDTH) / 2, 2 * SIZE_RADIUS_WIDTH, 2 * SIZE_RADIUS_WIDTH)];
//    _rotateView.backgroundColor = [UIColor cyanColor];
//    NSLog(@"%@",NSStringFromCGRect(_rotateView.frame));
//
//    [_toast addSubview:_rotateView];
//    
//     UIBezierPath *pathRotate= [UIBezierPath bezierPathWithArcCenter:CGPointMake(SIZE_RADIUS_WIDTH, SIZE_RADIUS_WIDTH) radius:SIZE_RADIUS_WIDTH startAngle:- M_PI_2 endAngle:( M_PI) clockwise:YES];
//    _rotateLayer = [CAShapeLayer layer];
//    _rotateLayer.path = pathRotate.CGPath;
//    _rotateLayer.fillColor = [UIColor clearColor].CGColor;
//    _rotateLayer.strokeColor = [UIColor whiteColor].CGColor;
//    _rotateLayer.lineWidth = 3;
//    _rotateLayer.lineCap = kCALineCapRound;
//    [_rotateView.layer addSublayer:_rotateLayer];
//    
//    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
////    rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//    rotateAnimation.fromValue = @0;
//    rotateAnimation.toValue = @(2*M_PI);
//
//    rotateAnimation.duration = 3;
//    rotateAnimation.repeatCount = HUGE;
//    rotateAnimation.removedOnCompletion = NO;
//    [_rotateView.layer addAnimation:rotateAnimation forKey:nil];
//
    
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(rect.size.height/2, rect.size.height/2) radius:60 startAngle:-M_PI_2 endAngle:M_PI clockwise:YES];
    
    _rotateLayer = [CAShapeLayer layer];
    _rotateLayer.path = path.CGPath;
    _rotateLayer.fillColor = [UIColor clearColor].CGColor;
    _rotateLayer.strokeColor = [UIColor purpleColor].CGColor;
    _rotateLayer.lineWidth = 3;
    _rotateLayer.lineCap = kCALineCapRound;
    [self.layer addSublayer:_rotateLayer];


    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    rotateAnimation.fromValue = @0;
    rotateAnimation.toValue = @(2*M_PI);

    rotateAnimation.duration = 3;
    rotateAnimation.repeatCount = HUGE;
    rotateAnimation.removedOnCompletion = NO;
    [self.layer addAnimation:rotateAnimation forKey:nil];

    

    
    
    
}

@end

你可能感兴趣的:(iOS刷新控件的旋转效果)