需求

来源:互联网 发布:用记事本编写java 编辑:程序博客网 时间:2024/03/29 16:50

可以用单纯的 图层加核心动画实现一个等待旋转的效果:


#import "LoadingCircleButton.h"@interface LoadingCircleButton ()@property (nonatomic,strong) CAShapeLayer *loadingLayer;@property (nonatomic,strong) UIButton *button;@end@implementation LoadingCircleButton- (instancetype)initWithFrame:(CGRect)frame{    if(self = [super initWithFrame:frame])    {        _button = [UIButton buttonWithType:UIButtonTypeCustom];        _button.frame = self.bounds;        [_button setTitle:@" " forState:UIControlStateNormal];        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];        _button.titleLabel.font = [UIFont systemFontOfSize:13.f];        [self addSubview:_button];        [_button addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];    }    return self;}- (void)clickBtn{    [self loadingAnimation];}- (void)loadingAnimation{    _loadingLayer = [CAShapeLayer layer];    _loadingLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);    _loadingLayer.fillColor = [UIColor clearColor].CGColor;    _loadingLayer.strokeColor = [UIColor whiteColor].CGColor;    _loadingLayer.lineWidth = 2;    _loadingLayer.path = [self drawLoadingBezierPath].CGPath;    [self.layer addSublayer:_loadingLayer];        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];    basicAnimation.fromValue = @(0);    basicAnimation.toValue = @(M_PI*2);    basicAnimation.duration = 0.5;    basicAnimation.repeatCount = LONG_MAX;    [_loadingLayer addAnimation:basicAnimation forKey:@"loadingAnimation"];        [self performSelector:@selector(stopLoadingAnimation) withObject:self afterDelay:5];}- (void)stopLoadingAnimation{    [_loadingLayer removeFromSuperlayer];}- (UIBezierPath *)drawLoadingBezierPath{    CGFloat radius = self.bounds.size.height/2 - 3;    UIBezierPath *bezierPath = [UIBezierPath bezierPath];    [bezierPath addArcWithCenter:CGPointMake(0,0) radius:radius startAngle:M_PI/2 endAngle:M_PI/2+M_PI/2 clockwise:YES];    return bezierPath;}@end


原创粉丝点击