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