开发者

How to stop executing FOR loop until code is completed

开发者 https://www.devze.com 2023-03-10 08:24 出处:网络
I made this thing. It has no purpose other than for demonstration to myself. Essentially, I\'m moving a view that I created around the screen in a loop. The problem is that it doesn\'t wait until it\'

I made this thing. It has no purpose other than for demonstration to myself. Essentially, I'm moving a view that I created around the screen in a loop. The problem is that it doesn't wait until it's done with one animation before starting the other, and so the view bypasses the first two and ends up at the last toastRect after the for loop is done, and it doesn't keep moving after that. How can I force the code to complete each animation befor开发者_如何学编程e moving on to the next?

int i;
    for (i=0; i < 100; i++) {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:9];
        toast.view.frame = toastRect1;
        [UIView commitAnimations];

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:9];
        toast.view.frame = toastRect2;
        [UIView commitAnimations];

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:9];
        toast.view.frame = toastRect;
        [UIView commitAnimations];
        NSLog(@"%d",i);

    }

}


I think you need to use + (void)setAnimationDidStopSelector:(SEL)selector. Take a closer look at http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html if you want.


You can use the setAnimationDidStopSelector method to chain the animations. Check the docs for details: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html%23//apple_ref/occ/clm/UIView/setAnimationDidStopSelector:

0

精彩评论

暂无评论...
验证码 换一张
取 消