开发者

UIView animations in a loop

Is there a reason why animations do not work in a loop? I know you can do the command:

[UIView setAnimationRepeatCount: someNumber];

which was working fine until I put actual different pdfs to be loaded. So before, with just one pdf that would always get loaded, when I did the animation say 5 times to go to the last document, the animations would look correct. But now that I have 5 different pdfs, it will flip the page 5 times with the CurlUp but it will keep showing the 1st pdf in the list, and then at the end load the proper one. So I thought I could do it in a loop, keeping tr开发者_Python百科ack of the location in the array, and load each pdf at each location as i flip through the pages so it gives the user the feeling of where they are in the stack of pdfs. But when I put the core animation code in a loop, it basically shows the animation once, but does get to the correct document.


From my understanding of what you're doing, it sounds like you're actually running all 5 animations at once. When you perform an animation block in a for-loop, the runloop doesn't iterate between each loop and only the last animation gets run.

What you'll need to use is use completion calls and "chain" them together. So you run the first animation, then on that completion call you run the second, then on that call the third, etc. Much more lengthy to code, but this will work.

Would be easier to use blocks in this scenario, then all your animation code will be in one place instead of having to spread it all across a bunch of methods for each animation. You would just nest the blocks, in the first animation's completion block you'd spawn the second, then the third, etc.

Here's an example using the blocks method. It's a little hard to read, but you'll get the idea. You might be able to create a single animation block and reuse it in each completion method, but I don't know all of the catches of doing that, and this is much easier to read when you're first trying to understand it.

Note that I haven't actually tried this or tested it, but I think it will do what you want.

    [UIView animateWithDuration:1.0 animations:^(void) {
        // Animation changes go here
        // blah.alpha = 1.0
        // blah.position = CGPointMake
        // etc
    } completion:^(BOOL finished) {
        // Start next animation which will run with this one finishes
        [UIView animateWithDuration:1.0 animations:^(void) {
            // Animation changes go here
            // blah.alpha = 1.0
            // blah.position = CGPointMake
            // etc
        } completion:^(BOOL finished) {
            // Start next animation which will run with this one finishes
            [UIView animateWithDuration:1.0 animations:^(void) {
                // Animation changes go here
                // blah.alpha = 1.0
                // blah.position = CGPointMake
                // etc
            } completion:^(BOOL finished) {
                // Start next animation which will run with this one finishes
                [UIView animateWithDuration:1.0 animations:^(void) {
                    // Animation changes go here
                    // blah.alpha = 1.0
                    // blah.position = CGPointMake
                    // etc
                } completion:nil];
            }];
        }];
    }];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜