【问题标题】:how can i create a pulsating animation for UIButton iOS?如何为 UIButton iOS 创建一个脉动动画?
【发布时间】:2015-06-05 18:28:22
【问题描述】:

我正在尝试为 UIButton 创建一个脉动动画 到目前为止,这些是我的代码,但仍然无法执行此链接中显示的确切动画

CSS pulsating button

我的代码

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.backgroundColor = [UIColor lightGrayColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       [button addTarget:self
               action:@selector(method)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 160.0);
    CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulseAnimation.duration = .5;
    pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
    pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulseAnimation.autoreverses = YES;
    pulseAnimation.repeatCount = FLT_MAX;
    [button.layer addAnimation:pulseAnimation forKey:nil];
    button.layer.cornerRadius=button.frame.size.width / 2;
    ;
    button.layer.masksToBounds = YES;

    [self.view addSubview:button];

【问题讨论】:

    标签: ios objective-c animation uibutton


    【解决方案1】:

    一个很好的控件,可以根据需要进行调整。 https://github.com/zackhsuan/ZKPulseView

    【讨论】:

      【解决方案2】:

      试试这个。

      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
      button.frame = CGRectMake(0, 0, 100, 100);
      button.center = self.view.center;
      [button setTitle:@"Button" forState:UIControlStateNormal];
      
      UIView *c = [[UIView alloc] initWithFrame:button.bounds];
      c.backgroundColor = [UIColor blueColor];
      c.layer.cornerRadius = 50;
      [button addSubview:c];
      [button sendSubviewToBack:c];
      
      UIView *f = [[UIView alloc] initWithFrame:button.bounds];
      f.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.3];
      f.layer.cornerRadius = 50;
      [button addSubview:f];
      [button sendSubviewToBack:f];
      
      CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
      pulseAnimation.duration = .5;
      pulseAnimation.toValue = [NSNumber numberWithFloat:1.1];
      pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
      pulseAnimation.autoreverses = YES;
      pulseAnimation.repeatCount = MAXFLOAT;
      [c.layer addAnimation:pulseAnimation forKey:@"a"];
      [button.titleLabel.layer addAnimation:pulseAnimation forKey:@"a"];
      
      CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"opacity"];
      fade.toValue = @0;
      CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
      pulse.toValue = @2;
      CAAnimationGroup *group = [CAAnimationGroup animation];
      group.animations = @[fade,pulse];
      group.duration = 1.0;
      group.repeatCount = MAXFLOAT;
      [f.layer addAnimation:group forKey:@"g"];
      
      [self.view addSubview:button];
      

      【讨论】:

        猜你喜欢
        • 2011-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-18
        • 2019-09-28
        • 1970-01-01
        • 1970-01-01
        • 2014-05-19
        相关资源
        最近更新 更多