思想:用一個貝塞爾曲線畫一個圓讹开,然后用一個CAShapLayer對象的路徑等于貝塞爾曲線的路徑寄锐。將這個layer對象做為當前視圖的子layer击蹲,然后在做旋轉(zhuǎn)動畫毡琉。
<h4>旋轉(zhuǎn)動畫一定要加到當前的view.layer上</h4>
總的來說就是旋轉(zhuǎn)這個view而不是旋轉(zhuǎn)這個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