動(dòng)畫有點(diǎn)抖s
gif.gif
給UIView添加一個(gè)分類
#import <UIKit/UIKit.h>
@interface UIView (RadarAnimation)
@property(nonatomic,strong)UIColor *radarColor; //擴(kuò)散顏色
@property(nonatomic,assign)UIColor *radarBorderColor; //擴(kuò)散邊界顏色
-(void)addRadarAnimation;
@end
調(diào)用方法:
button.radarColor = LBColor(237, 174, 130, 1);
button.radarBorderColor = LBColor(237, 174, 130, 0.5);
[button addRadarAnimation];
添加動(dòng)畫的方法,創(chuàng)建三個(gè)layer,只不過,開始動(dòng)畫的時(shí)間要錯(cuò)開形成這個(gè)效果:
-(void)Animation{
NSInteger pulsingCount = 3;
double animationDuration = 2;
CALayer * animationLayer = [[CALayer alloc]init];
for (int i = 0; i < pulsingCount; i++) {
CALayer * pulsingLayer = [[CALayer alloc]init];
pulsingLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
pulsingLayer.backgroundColor = self.radarColor.CGColor;
pulsingLayer.borderColor = self.radarBorderColor.CGColor;
pulsingLayer.borderWidth = 1.0;
pulsingLayer.cornerRadius = self.frame.size.height/2;
CAMediaTimingFunction * defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CAAnimationGroup * animationGroup = [[CAAnimationGroup alloc]init];
animationGroup.fillMode = kCAFillModeBoth;
animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration/(double)pulsingCount;
animationGroup.duration = animationDuration;
animationGroup.repeatCount = HUGE_VAL;
animationGroup.timingFunction = defaultCurve;
CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.autoreverses = NO;
scaleAnimation.fromValue = [NSNumber numberWithDouble:1];
scaleAnimation.toValue = [NSNumber numberWithDouble:1.5];
CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.values = @[[NSNumber numberWithDouble:1.0],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.3],[NSNumber numberWithDouble:0.0]];
opacityAnimation.keyTimes = @[[NSNumber numberWithDouble:0.0],[NSNumber numberWithDouble:0.25],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:1.0]];
animationGroup.animations = @[scaleAnimation,opacityAnimation];
[pulsingLayer addAnimation:animationGroup forKey:@"pulsing"];
[animationLayer addSublayer:pulsingLayer];
}
animationLayer.zPosition = -1;//重新加載時(shí),使動(dòng)畫至底層
[self.layer addSublayer:animationLayer];
}
為了防止動(dòng)畫進(jìn)入后臺(tái)假死,需要監(jiān)聽通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterPlayground) name:UIApplicationDidBecomeActiveNotification object:nil];
demo 地址:漫漫的demo