您可以使用选项对UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat告诉Core Animation您希望标签连续淡入和淡出,而不是嵌套块并手动重新启动动画.
- (void)startAnimatingLabel
{
self.label.alpha = 0;
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
self.label.alpha = 1;
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
要停止运行动画,只需从标签的图层中删除它们即可.
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
[self.label.layer removeAllAnimations];
self.label.alpha = 0;
[self presentNewView];
}
Run Code Online (Sandbox Code Playgroud)
编辑:一个不太突然的完成方式是从当前视图状态动画到最后一个(这将中断当前,重复动画).
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.label.alpha = 0;
} completion:^(BOOL finished){
[self presentNewView];
}];
}
Run Code Online (Sandbox Code Playgroud)
您可以在循环中放置一对链式动画,或者每次调用一个包含链式动画的函数,直到遇到用户点击.
通过链式动画,我的意思是这样的(您可以设置动画持续时间以满足您的需要):
myLabel.alpha = 0.0;
[UIView animateWithDuration:1.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
myLabel.alpha = 1.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
myLabel.alpha = 0.0;
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
}];
Run Code Online (Sandbox Code Playgroud)
上面的代码将首先淡出您的标签,然后将其淡出.你可以把它放在一个函数中并调用它,直到你遇到用户点击.
| 归档时间: |
|
| 查看次数: |
3644 次 |
| 最近记录: |