在我的Xcode项目中,我在xib上放置了一个标签。
我想要不断淡出标签,直到用户点击屏幕。当这种情况发生时,我希望出现一个新的视图。
有人能建议怎么做淡入/淘汰吗?
发布于 2013-07-24 01:53:15
您可以在循环中放置一对链接动画,或者每次都调用保存链接动画的函数,直到遇到用户点击为止。
所谓连锁动画,我指的是这样的东西(您可以根据您的需要设置动画持续时间):
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!");
}];
}];上述代码将首先淡出您的标签,然后淡出。你可以把它放到一个函数中,然后调用它,直到你遇到用户点击。
发布于 2013-07-24 02:17:40
而不是嵌套块和手动重新启动您的动画,您可以使用选项对UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat告诉核心动画,您希望标签不断淡入和退出。
- (void)startAnimatingLabel
{
self.label.alpha = 0;
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
self.label.alpha = 1;
} completion:nil];
}要停止动画的运行,只需将它们从标签层中删除即可。
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
[self.label.layer removeAllAnimations];
self.label.alpha = 0;
[self presentNewView];
}编辑:编辑:一个较不突然的完成方式是从当前视图状态动画到最后一个视图状态(这将中断当前的、重复的动画)。
- (IBAction)tap:(UITapGestureRecognizer *)sender
{
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.label.alpha = 0;
} completion:^(BOOL finished){
[self presentNewView];
}];
}发布于 2013-07-24 03:03:45
创建一个CABasicAnimation并将其添加到您的标签中:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = @(1.0f);
animation.toValue = @(0.1f);
animation.repeatCount = INFINITY;
animation.duration = 0.75;
animation.autoreverses = YES;
[label.layer addAnimation:animation];单击按钮时,只需获得指向该标签的指针并删除所有动画:
[label.layer removeAllAnimations];https://stackoverflow.com/questions/17823902
复制相似问题