```Objective-C
@interface LoadingAnimationView : UIView
@end
@implementation LoadingAnimationView
- (instancetype)init {
if (self = [super init]) {
// 设置动画效果时间
self.alpha = 0.5;
self.animationDuration = 3.0f;
}
return self;
}
- (void)drawRect:(CGRect)rect {
// 绘制对应的加载动画
// 例如:一个圆形、一个直线等
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(rect.size.width/2, 10)];
[path addLineToPoint:CGPointMake(rect.size.width/2, rect.size.height - 10)];
[path addArcWithCenter:CGPointMake(rect.size.width/2, 0) radius:rect.size.height/2 startAngle:M_PI endAngle:3*M_PI clockwise:YES];
[UIColor.lightGrayColor setStroke];
[path stroke];
}
- (void)startAnimating {
// 加载动画,需要实现一个简单的动画组
// 比如一个圆形,从右边移动到左边,一直重复
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position.x"];
move.fromValue = [NSNumber numberWithFloat:self.frame.size.width/2 + self.frame.size.height/2 - 10];
move.toValue = [NSNumber numberWithFloat:self.frame.size.width/2 - self.frame.size.height/2 + 10];
move.duration = self.animationDuration;
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotation.fromValue = [NSNumber numberWithDouble:0];
rotation.toValue = [NSNumber numberWithDouble:2*M_PI];
rotation.duration = self.animationDuration;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[move, rotation];
group.duration = self.animationDuration;
group.repeatCount = 0;
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[self.layer addAnimation:group forKey:nil];
}
@end
```
网友评论