效果图
instancetype
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self creatCircleAnimation];
}
return self;
}
creatCircleAnimation
-(void)creatCircleAnimation
{
self.backgroundColor = [UIColor colorWithWhite:0.8f alpha:0.5f];
CGFloat x = self.frame.size.width * 0.5;
CGFloat y = self.frame.size.height * 0.5;
CAShapeLayer *cycleLayer = [CAShapeLayer layer];
cycleLayer.lineCap = kCALineCapRound;
cycleLayer.lineJoin = kCALineJoinRound;
cycleLayer.lineWidth = 5.f;
cycleLayer.fillColor = [UIColor clearColor].CGColor;
cycleLayer.strokeColor = [UIColor orangeColor].CGColor;
cycleLayer.strokeEnd = 0.f;
cycleLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x - kRadius, y - kRadius, kRadius * 2, kRadius * 2)].CGPath;
[self.layer addSublayer:cycleLayer];
CABasicAnimation *strokeStartAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
strokeStartAnimation.fromValue = @(-1);
strokeStartAnimation.toValue = @(1);
CABasicAnimation *strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
strokeEndAnimation.fromValue = @(0);
strokeEndAnimation.toValue = @(1.0);
CAAnimationGroup *animationGroup = [CAAnimationGroup new];
animationGroup.duration = 1.f;
animationGroup.repeatCount = INFINITY;
animationGroup.animations = @[strokeStartAnimation,strokeEndAnimation];
animationGroup.removedOnCompletion = NO;
[cycleLayer addAnimation:animationGroup forKey:@"animationGroup"];
CALayer *testLayer = [CALayer layer];
testLayer.backgroundColor = [UIColor clearColor].CGColor;
testLayer.frame = self.bounds;
[self.layer addSublayer:testLayer];
[testLayer addSublayer:cycleLayer];
// [self setAnchorPoint:CGPointMake(0.5, 0.5) forView:self];
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.rotation.z";
animation.duration = 1.f;
animation.fromValue = @(0);
animation.toValue = @(2*M_PI);
animation.repeatCount = INFINITY;
animation.removedOnCompletion = NO; // 防止点击HOME键再进入的时候,动画停止
[testLayer addAnimation:animation forKey:nil];
// 添加label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kRadius * 2 - 20, 25)];
label.text = @"正在加载···";
label.textColor = [UIColor orangeColor];
label.adjustsFontSizeToFitWidth = YES;
label.center = CGPointMake(x, y);
[self addSubview:label];
}
remove
-(void)removeSelf
{
[UIView animateWithDuration:0.2f animations:^{
self.alpha = 0.f;
}completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
使用
LoadingView *loading = [[LoadingView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:loading];