Skip to content

Instantly share code, notes, and snippets.

@daoseng33
Created August 10, 2017 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daoseng33/9a7316a73443f19fc9ef1b50ce237dfa to your computer and use it in GitHub Desktop.
Save daoseng33/9a7316a73443f19fc9ef1b50ce237dfa to your computer and use it in GitHub Desktop.
仿Nice tag 雷達閃爍動畫
#pragma mark -- Animation
//动画停止执行的delegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
if ([[anim valueForKey:@"TransformScale"] isEqualToString:@"scaleAnimation"]) {
//清除上次的shapeLayer 防止内存泄露
for (CAShapeLayer *shapeLayer in shapeLayers) {
[shapeLayer removeAnimationForKey:kGroupAnimation];
[shapeLayer removeFromSuperlayer];
}
[shapeLayers removeAllObjects];
isAperture = NO;
[self addApertureAnimationDuration:.6f beginTime:0.5f];
[self addApertureAnimationDuration:.6f beginTime:1.f];
} else if ([[anim valueForKey:@"animation"] isEqualToString:@"animation"]) {
if (!isAperture) {
[imageView.layer removeAnimationForKey:kAnimatiomName];
[self addAnimationDuration:0.7f beginTime:0.5f];
isAperture = YES;
}
}
}
//添加动画
- (void)addAnimationDuration:(CGFloat)duration beginTime:(CGFloat)beginTime
{
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:1.f];
scaleAnimation.toValue = [NSNumber numberWithFloat:1.5f];
scaleAnimation.duration = duration;
scaleAnimation.beginTime = CACurrentMediaTime() + beginTime;
scaleAnimation.delegate = self;
[scaleAnimation setValue:@"scaleAnimation" forKey:@"TransformScale"];
[imageView.layer addAnimation:scaleAnimation forKey:kAnimatiomName];
}
//添加光晕效果
- (void)addApertureAnimationDuration:(CGFloat)duration beginTime:(CGFloat)beginTime
{
CGRect frame = CGRectMake(-CGRectGetMidX(imageView.bounds), -CGRectGetMidY(imageView.bounds), CGRectGetWidth(imageView.bounds), CGRectGetHeight(imageView.bounds));
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:CGRectGetWidth(imageView.bounds)];
CGPoint position = [imageView.superview convertPoint:imageView.center fromView:imageView.superview];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.55f].CGColor;
shapeLayer.strokeColor = [UIColor clearColor].CGColor;
shapeLayer.lineWidth = 0.f;
shapeLayer.path = path.CGPath;
shapeLayer.position = position;
shapeLayer.opacity = 0.f;
[imageView.superview.layer addSublayer:shapeLayer];
[shapeLayers addObject:shapeLayer];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5f, 1.f, 1.f)];
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = @1;
opacityAnimation.toValue = @0;
CAAnimationGroup *animation = [CAAnimationGroup animation];
animation.animations = @[scaleAnimation,opacityAnimation];
animation.duration = duration;
animation.beginTime = CACurrentMediaTime() + beginTime;
animation.delegate = self;
[animation setValue:@"animation" forKey:@"animation"];
[shapeLayer addAnimation:animation forKey:kGroupAnimation];
}
//停止动画 并将光晕移除
- (void)removeAnimation
{
[imageView.layer removeAnimationForKey:kAnimatiomName];
for (CAShapeLayer *shapeLayer in shapeLayers) {
[shapeLayer removeAnimationForKey:kGroupAnimation];
[shapeLayer removeFromSuperlayer];
}
[shapeLayers removeAllObjects];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment