Is it posible to do multiple animations on UIView without using completion block

强颜欢笑 提交于 2019-12-11 11:57:38

问题


This is simple question just for illustrating point, my concrete code is much more complicated.

Let say that I have UILabel that is position on TOP LEFT.
With this code, I will move it to TOP RIGHT:

    // labelMove is moved to TOP RIGHT
    [UIView animateWithDuration:1.0f
                          delay:2.0f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
         labelMove.center = CGPointMake(250.0f, 40.0f);
     }
                     completion:nil];

If I have this code:

// labelMove is moved to TOP RIGHT
[UIView animateWithDuration:1.0f
                      delay:2.0f
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^
 {
     labelMove.center = CGPointMake(250.0f, 40.0f);
 }
                 completion:nil];

// labelMove is moved to BOTTOM RIGHT
[UIView animateWithDuration:1.0f
                      delay:2.0f
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^
 {
     labelMove.center = CGPointMake(250.0f, 450.0f);
 }
                 completion:nil];

It will not start from TOP LEFT, but from TOP RIGHT, and then go BOTTOM RIGHT.
Somehow first animation is not animated, only the last one.
I do not get it why, but that is not so important.
I can put second in completion: block in first and then it will work.
Like this code.

// labelMove is moved to TOP RIGHT
[UIView animateWithDuration:1.0f
                      delay:2.0f
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^
 {
     labelMove.center = CGPointMake(250.0f, 40.0f);
 }
                 completion:^(BOOL finished){
                     // labelMove is moved to BOTTOM RIGHT
                     [UIView animateWithDuration:1.0f
                                           delay:2.0f
                                         options:UIViewAnimationOptionCurveEaseOut
                                      animations:^
                      {
                          labelMove.center = CGPointMake(250.0f, 450.0f);
                      }
                                      completion:nil];
                 }];

QUESTION
Is it posible to have effect from last code but without using completion block in any way.
That is very important for me.
I try with CABasicAnimation, but could not do it, maybe somebody with more experience can ?
I am sure that there must be some way, but I just can not figure it out.


回答1:


Your code with no completion block does not work because you are attempting to animate the label to two different locations at exactly the same point in time. This is physically impossible.

When you run that second animation inside a completion block, you are running it after the first animation, instead of at the same time. To achieve the same effect, dispatch the second animation so that it is performed after the first animation. Since the first animation will take 2.0 delay + 1.0 duration = 3.0 seconds to complete, dispatch after 3.0 seconds. See below.

// labelMove is moved to TOP RIGHT
[UIView animateWithDuration:1.0f
                      delay:2.0f
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^
 {
     labelMove.center = CGPointMake(250.0f, 40.0f);
 }
                 completion:nil];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    // labelMove is moved to BOTTOM RIGHT
    [UIView animateWithDuration:1.0f
                          delay:2.0f
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^
     {
         labelMove.center = CGPointMake(250.0f, 450.0f);
     }
                     completion:nil];
});

Just be aware that without using the completion block, you cannot insure the first animation is truly finished animating. In practice this should usually be fine.




回答2:


You could use a CAKeyframeAnimation, and create a path using the two points.

Here are some examples from apple:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html



来源:https://stackoverflow.com/questions/24267091/is-it-posible-to-do-multiple-animations-on-uiview-without-using-completion-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!