iOS 플러시 컨트롤 회전 효과

3354 단어
사상: 베셀 곡선으로 원을 그리고 CAShap Layer 대상의 경로를 베셀 곡선의 경로와 같다.이 layer 대상을 현재 보기의 하위 layer로 만들고 회전 애니메이션을 합니다

회전 애니메이션은 반드시 현재view에 추가해야 합니다.layer 위

전체적으로 이 뷰를 돌리는 것이지 이 CAShap Layer 대상을 돌리는 것이 아니다.

#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

좋은 웹페이지 즐겨찾기