【问题标题】:Make uibutton pulsate.让 uibutton 跳动。
【发布时间】:2012-07-12 05:07:56
【问题描述】:

我正在尝试通过在视图加载后来回更改 alpha 级别来使其中包含图像的 uibutton 缓慢脉动...

目前我正在这样做,但它什么也没做......

-(void)loopdyloop
{
    while ( myCount < 1000 )
    {

        [UIView animateWithDuration:3.0
                              delay:0.0f
                            options:UIViewAnimationCurveEaseOut
                         animations:^{


                             iconButton.alpha = 0.0;


                         } completion:^(BOOL finished) {
                             if (finished) {

                                 NSLog(@"animated");
                             }
                         }];


        [UIView animateWithDuration:3.0
                              delay:0.0f
                            options:UIViewAnimationCurveEaseOut
                         animations:^{


                             iconButton.alpha = 1.0;


                         } completion:^(BOOL finished) {
                             if (finished) {

                                 NSLog(@"animated");
                             }
                         }];




        myCount++;
    }

}

这个方法是从viewdidload方法中调用的,

我知道这很粗糙,但这是我最好的尝试,如果您对我如何实现这一点有任何想法,将不胜感激。

【问题讨论】:

    标签: iphone ios uibutton uiviewanimation


    【解决方案1】:

    如何在viewDidLoad 的按钮层中应用它:

    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;
    [YOURButton.layer addAnimation:pulseAnimation forKey:nil];
    

    你可以尝试一下。

    【讨论】:

    • 我实际上已经看到了类似于此代码的内容.. 但我不知道如何将其应用于 uibutton。我认为不仅仅是 mybutton.layer :P:P 感谢您的提醒!动画太棒了! :P
    • 哈哈哈,我也是。我想你只需要在你的按钮层中应用它。祝你好运:)
    • 这真的很棒但是,当离开应用程序并返回时,它会停止动画。我做到了,它是自己的方法,我在 viewDidLoad 和 viewDidAppear 中调用它,但同样的事情。
    • 某些原因...当我将视图控制器推入堆栈然后添加 uibutton 它可以工作...但是当我使用按钮呈现视图控制器时,它不会动画..
    【解决方案2】:

    如果您希望它永远重复,您可以添加动画选项以自动反转和重复,如下所示:

    [UIView animateWithDuration:3.0
                          delay:0.0f
                        options:UIViewAnimationCurveEaseOut | 
                                UIViewAnimationOptionRepeat | 
                                UIViewAnimationOptionAutoreverse
                     animations:^{
                         iconButton.alpha = 0.0;
                     } 
                     completion:^(BOOL finished) {
                         // You could do something here
                     }];
    

    它会导致 alpha 首先动画到 0.0,然后回到原来的 (1.0),然后一遍又一遍地做这两件事......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多