Objective-C自定义加载菊花UIActivityIndicatorView

自定义Objective-C加载菊花视图
这篇博客介绍了如何在Objective-C中创建一个名为HalfCircleActivityIndicatorView的自定义加载菊花视图,提供了头文件和源文件的代码示例,并展示了使用效果。还提供了GitHub项目的链接供进一步参考。

很简单,直接上代码。
新建一个继承自UIView的类HalfCircleActivityIndicatorView
头文件(.h)代码:

#import <UIKit/UIKit.h>

@interface HalfCircleActivityIndicatorView : UIView

@property (strong, nonatomic) UIColor *color;
@property (nonatomic) BOOL hidesWhenStopped;

- (void)startAnimating;//开始动画
- (void)stopAnimating;//停止动画
- (BOOL)isAnimating;//动画状态

@end

源文件(.m)代码:

#import "HalfCircleActivityIndicatorView.h"

@interface HalfCircleActivityIndicatorView ()
@property (nonatomic) BOOL isAnimation;
@end

@implementation HalfCircleActivityIndicatorView

@synthesize hidesWhenStopped = _hidesWhenStopped;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = self.frame.size.width / 2;
        self.hidesWhenStopped = NO;
        self.color = [UIColor blackColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (int i = 0; i < 360; i++) {
        CGFloat beginAngle = M_PI * 2 / 360 * i;
        CGFloat toAngle  = beginAngle + M_PI * 2 / 360;

        CGFloat alpha = 0;
        if (i < 180) {
            alpha = 1.0 / 180 * (i + 1);
        } else {
            alpha = 1.0 / 180 * (i - 180 + 1);
        }
        [self drawCircleWithContext:context beginAngle:beginAngle toAngle:toAngle color:_color alpha:alpha];
    }
}

- (void)drawCircleWithContext:(CGContextRef)context beginAngle:(CGFloat)beginAngle toAngle:(CGFloat) toAngle color:(UIColor *)color alpha:(CGFloat)alpha {
    CGContextSaveGState(context);
    CGContextBeginPath(context);
    CGFloat r, g, b;
    [color getRed:&r green:&g blue:&b alpha:nil];
    CGContextSetRGBStrokeColor(context, r, g, b, alpha);
    CGContextSetLineWidth(context, 1.0);
    CGContextAddArc(context, self.frame.size.width / 2, self.frame.size.height / 2, self.frame.size.width / 2 - 1, beginAngle, toAngle, 0);
    CGContextDrawPath(context, kCGPathStroke);
}

//开始动画,沿着z轴方向旋转
- (void)startAnimating {
    if ([self isAnimating]) {
        return;
    }

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.fromValue = @(0);
    animation.toValue = @(M_PI * 2);
    animation.duration = 1.f;
    animation.repeatCount = INT_MAX;

    [self.layer addAnimation:animation forKey:@"rotate"];

    _isAnimation = YES;
    self.hidden = NO;
}
//停止动画
- (void)stopAnimating {
    [self.layer removeAnimationForKey:@"rotate"];
    _isAnimation = NO;
    if (_hidesWhenStopped) {
        self.hidden = YES;
    }
}
//动画状态
- (BOOL)isAnimating {
    return _isAnimation;
}

-(void)setHidesWhenStopped:(BOOL)hidesWhenStopped {
    if (_hidesWhenStopped != hidesWhenStopped) {
        _hidesWhenStopped = hidesWhenStopped;
    }
    if (hidesWhenStopped &&
        NO == [self isAnimating]) {
        self.hidden = YES;
    }
    else {
        self.hidden = NO;
    }
}

@end

使用:

- (IBAction)action:(id)sender {
    UIButton *button = (UIButton *)sender;
    if ([self.loading isAnimating]) {
        [self.loading stopAnimating];
        [button setTitle:@"开始" forState:UIControlStateNormal];
    }else{
        [self.loading startAnimating];
        [button setTitle:@"停止" forState:UIControlStateNormal];
    }
}

效果:
这里写图片描述
github地址:
GitHub demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值