項目中有用到關(guān)于雷達(dá)擴(kuò)散的那種動畫效果嗤锉,研究了下源祈,在這里分享一下煎源。
直接上demo源碼。
//定義兩個屬性
@property (nonatomic, strong) CALayer *layer_1;
@property (nonatomic, strong) CALayer *layer_2;
//分別進(jìn)行懶加載
- (CALayer *)layer_1{
if (!_layer_1) {
_layer_1 = [CALayer layer];
// _layer_1.delegate = self;
}
return _layer_1;
}
- (CALayer *)layer_2{
if (!_layer_2) {
_layer_2 = [CALayer layer];
// _layer_2.delegate = self;
}
return _layer_2;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//你的view的中心位置香缺,在這里我自定義個MyView手销,作為視圖的中心位置
UIView *MyView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2, 2, 2)];
MyView.backgroundColor = [UIColor redColor];
[self.view addSubview:MyView];
self.layer_1.bounds = MyView.bounds;
self.layer_1.position = MyView.center;
self.layer_1.cornerRadius = MyView.frame.size.width / 2;
self.layer_1.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:112 / 255.0 blue:192 / 255.0 alpha:1.0].CGColor;
[self.view.layer addSublayer:self.layer_1];
//透明度的添加,從深到淺
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = [NSNumber numberWithFloat:0.7];
opacityAnimation.toValue = [NSNumber numberWithFloat:0.01];
//x图张,y軸一起延伸縮放
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:0.5];
scaleAnimation.toValue = [NSNumber numberWithFloat:self.view.frame.size.width / 2];
scaleAnimation.duration = 3.0f;
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//設(shè)置兩個動畫锋拖,用個動畫組
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = 3.0f;
animationGroup.fillMode = kCAFillModeBackwards;
animationGroup.autoreverses = NO; //是否重播,原動畫的倒播
animationGroup.repeatCount = NSNotFound;//HUGE_VALF; //HUGE_VALF,源自math.h
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.delegate = self;
animationGroup.removedOnCompletion = NO;
//將動畫效果添加進(jìn)動畫組里
[animationGroup setAnimations:[NSArray arrayWithObjects:opacityAnimation, scaleAnimation, nil]];
[self.layer_1 addAnimation:animationGroup forKey:nil];
self.layer_2.bounds = MyView.bounds;
self.layer_2.position = MyView.center;
self.layer_2.cornerRadius = MyView.frame.size.width/2.0;
self.layer_2.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:112 / 255.0 blue:192 / 255.0 alpha:1.0].CGColor;
[self.view.layer insertSublayer:self.layer_2 below:self.layer_1];
animationGroup.beginTime = CACurrentMediaTime() + 1.5;
animationGroup.fillMode = kCAFillModeBackwards;
animationGroup.removedOnCompletion = NO;
[self.layer_2 addAnimation:animationGroup forKey:nil];
}
當(dāng)然了祸轮,還有代理方法
- (void)animationDidStart:(CAAnimation *)anim{
NSLog(@"動畫開始了");
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
NSLog(@"動畫停止了");
}
效果圖如下:
如果你也喜歡的話兽埃,點個贊吧~