很简单,直接上代码。
新建一个继承自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
自定义Objective-C加载菊花视图

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

被折叠的 条评论
为什么被折叠?



