UIView在移动时淡入并淡出

时间:2013-02-08 19:07:51

标签: ios animation uiview

为视图设置动画非常简单:

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

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

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

2 个答案:

答案 0 :(得分:12)

您需要做的就是背靠背应用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];
                                      }];
                 }];

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

希望这有帮助

答案 1 :(得分:2)

将初始alpha = 0置于动画块之外。

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