【问题标题】:Core Animation to flash score (UILabel)Core Animation to flash score (UILabel)
【发布时间】:2011-03-24 20:47:40
【问题描述】:

我在 iPhone 上的游戏中使用核心动画来闪现中间分数(使用 UILabel)。我需要它重复特定命中的特定计数。
分数需要闪烁特定计数,然后消失。
所以它应该从 alpha 0.0 -> 1.0 -> 0.0

下面是我试图实现这一点的代码。

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:repeatCount];
[UIView setAnimationRepeatAutoreverses:YES];
playerScore.alpha = 1.0f;
[UIView commitAnimations];  

问题是动画结束后,alpha回到1.0

有什么建议吗?

【问题讨论】:

    标签: ios core-animation


    【解决方案1】:

    我会在UIView 上使用更强大的animateWithDuration:delay:options:animations:completion: 方法。见View programming guideUIView class reference

    更具体地说,它可能看起来像这样:该方法需要两个块:一个用于动画本身,另一个块在动画完成后执行。乍一看可能有点奇怪,但这只是块语法。

    [UIView animateWithDuration:1.0 delay:0.f options:(UIViewAnimationOptionAutoreverse| UIViewAnimationOptionRepeat)
       animations:^{
       playerScore.alpha=1.f;
       } completion:^(BOOL finished){
       playerScore.alpha=0.f;
       }];
    

    此解决方案适用于 iOS 4 或更高版本。如果要在此之前定位版本,则必须使用委托回调。设置选择器在动画完成时执行,如下所示:

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(flashingDidStop:finished:context:)];
    //with the callback method
    - (void)flashingDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
    {
       //code to execute in your case
       playerScore.alpha = 0.f;
    }
    

    【讨论】:

    • 我可以在 iOS
    • @Dev 正确。块很酷,但只能从 iOS4 开始使用。我为 pre iOS4 添加了解决方案。更多可以在文档链接中找到。
    【解决方案2】:

    如果你需要它在 iOS

     - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
      playerScore.alpha = 0.0f;
    }
    
    ...
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationRepeatCount:repeatCount];
    [UIView setAnimationRepeatAutoreverses:YES];
    playerScore.alpha = 1.0f;
    [UIView commitAnimations];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多