ios - Core Animation to flash score(UILabel)

标签 ios core-animation

我正在使用核心动画在 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

有什么建议吗?

最佳答案

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

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

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

此解决方案适用于 iOS 版本 4 或更高版本。如果您想在此之前定位版本,则必须使用委托(delegate)回调。像这样设置动画完成时要执行的选择器:

[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 - Core Animation to flash score(UILabel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5418884/

相关文章:

iphone - 文本字段覆盖有背景,就像 Clear iPhone 应用程序一样

ios - 如何使用 SDWebImage - Swift 在 imageURL 为空时设置默认图像

objective-c - 转换从 View :toView:duration:options:completion: is not animating the transition

swift - CABasicAnimation 创建 CALayer 的空默认值副本

ios - 如何将从UIImagePickerViewController拾取的图像的路径保存到sqlite数据库并在以后访问? ios

ios - 阴影图像在第一个 viewWillAppear 上不起作用

ios - Objective-C block 参数问题 : This block declaration is not a prototype

iphone - 创建类似用户位置的动画 蓝色弹珠掉落

iphone - 有没有一种简单的方法可以确定两个变换是否相同?

cocoa - 为什么必须使用 CALayer 的presentationLayer 来进行 HitTest ?