【问题标题】:Fade in and fade out a UIView while it is moving移动时淡入淡出 UIView
【发布时间】:2013-01-24 15:15:36
【问题描述】:

动画视图很容易:

[UIView animateWithDuration:1.0
                     animations:^{theView.center = newCenter; theView.alpha = 0;}
                     completion:^(BOOL finished){
                         [theView removeFromSuperview];
                     }];

问题是当我将它添加为子视图时,我希望它淡入并且看起来它已经在移动了。现在它立即出现,然后移动并淡出。

所以,我需要将它的初始 alpha 设置为零,在它移动时快速淡出,然后淡出。这可能与 UIView 动画吗?我不能让两个相互竞争的动画块在同一个对象上工作,对吧?

【问题讨论】:

  • 我没有关注您:您的代码是典型的“滑动和淡出”示例。但是然后您谈论“当我将其添加为子视图时”。你在加吗?还是去掉?但是,在回答您的问题时,您不应该需要多个动画块。这只是你想要做什么的问题。
  • 这是一个类似的帖子,里面有很多很好的动画答案:stackoverflow.com/questions/5040494/…

标签: ios animation uiview


【解决方案1】:

您需要做的就是连续应用 2 个动画。像这样的东西::

theView.alpha = 0;
[UIView animateWithDuration:1.0
                 animations:^{
                     theView.center = midCenter;
                     theView.alpha = 1;
                 }
                 completion:^(BOOL finished){
                     [UIView animateWithDuration:1.0
                                      animations:^{
                                          theView.center = endCenter;
                                          theView.alpha = 0;
                                      }
                                      completion:^(BOOL finished){
                                          [theView removeFromSuperview];
                                      }];
                 }];

所以在第一秒它会在移动时出现,然后在下一秒它会淡出

希望对你有帮助

【讨论】:

  • 这应该可以工作,即使它需要一个额外的中心点来计算。我最终使用 CABasicAnimation 进行移动,使用 UIView 动画进行 alpha。
【解决方案2】:

将初始 alpha=0 放在动画块之外。

theView.alpha = 0;
[UIView animateWithDuration:1.0
                 animations:^{
                     theView.center = newCenter; 
                     theView.alpha = 1;
                 }
                 completion:^(BOOL finished){
                     // Do other things
                 }];

【讨论】:

  • 这适用于淡入,但我不知道为什么它不适用于淡出。?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
相关资源
最近更新 更多