脉冲动画的使用

脉冲动画, 就是类似一个圆圈向外扩散的效果,多用于搜索场景

一: 实现思路:
脉冲动画,实质上可以把扩散的圆圈看成一个CALayer, 那么我们只要在这个CALayer上做动画即可

二: 实现过程

  • 1: 首先我们先创建一个类CCJHaloLayer,继承于CALayer,radius半径,

     @interface CCJHaloLayer, : CALayer
    
     @property (nonatomic, assign) CGFloat radius;                  
     @property (nonatomic, assign) NSTimeInterval animationDuration; 
     @property (nonatomic, assign) NSTimeInterval pulseInterval; 
    
     @end
    
  • 2:当然在进一步做动画前,我们要在类的开头,导入QuartzCore等框架并生成一个动画组作为属性

    #import <QuartzCore/QuartzCore.h>
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    
    
    @interface CCJHaloLayer ()
    @property (nonatomic, strong) CAAnimationGroup *animationGroup;
    @end
    
  • 3: .m文件中的代码

    @implementation CCJHaloLayer
    
       - (id)init {
        self = [super init];
          if (self) {
      
                    self.contentsScale = [UIScreen mainScreen].scale;
                    self.opacity = 0;
    
                    self.radius = 200;
                    self.animationDuration = 1.5;
                    self.pulseInterval = 0;
                    self.backgroundColor = [[UIColor colorWithRed:0.000 green:0.478 blue:1.000 alpha:1] CGColor];
    
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
          
                        [self setupAnimationGroup];
    
                        if(self.pulseInterval != INFINITY) {
              
                            dispatch_async(dispatch_get_main_queue(), ^(void) {
                  
                              [self addAnimation:self.animationGroup forKey:@"pulse"];
                            });
                        }
                  });
                }
                  return self;
              }
    
    - (void)setRadius:(CGFloat)radius {
    
        _radius = radius;
      
        CGPoint tempPos = self.position;
    
        CGFloat diameter = self.radius * 2;
    
        self.bounds = CGRectMake(0, 0, diameter, diameter);
        self.cornerRadius = self.radius;
        self.position = tempPos;
    }
    
    - (void)setupAnimationGroup {
    
        CAMediaTimingFunction *defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    
        self.animationGroup = [CAAnimationGroup animation];
        self.animationGroup.duration = self.animationDuration + self.pulseInterval;
        self.animationGroup.repeatCount = INFINITY;
        self.animationGroup.removedOnCompletion = NO;
        self.animationGroup.timingFunction = defaultCurve;
    
        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
        scaleAnimation.fromValue = @0.0;
        scaleAnimation.toValue = @1.0;
        scaleAnimation.duration = self.animationDuration;
    
        CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        opacityAnimation.duration = self.animationDuration;
        opacityAnimation.values = @[@0.45, @0.45, @0];
        opacityAnimation.keyTimes = @[@0, @0.2, @1];
        opacityAnimation.removedOnCompletion = NO;
    
        NSArray *animations = @[scaleAnimation, opacityAnimation];
    
        self.animationGroup.animations = animations;
    }
    
    @end
    

三: 如何使用

比如所我想在点击按钮搜索的方法中进行动画,那么只需要加入如下代码即可

此处设置为动画的产生效果从按钮的中点开始, 首先为了防止重复点击,在方法的一开始就先移除以前的layer

  - (IBAction)searchBtnClick:(UIButton *)sender
  {
    [self.halo removeFromSuperlayer];
    self.halo = [CCJHaloLayer layer];
    self.halo.position = sender.center;
    [self.view.layer insertSublayer:self.halo below:sender.layer];
    self.first = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.halo removeFromSuperlayer];
    });
  }

这样就可以在任何你想要的位置添加脉冲动画了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,282评论 1 23
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,688评论 2 4
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,242评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,673评论 6 30
  • iOS动画篇之CoreAnimation动画 9月 22, 2016发布在Objective-C App如果想被大...
    白水灬煮一切阅读 2,284评论 0 0

友情链接更多精彩内容