Search code examples
iphonecocoa-touchuiscrollviewuipagecontrol

Why no animation in PageControl project?


I'm using the PageControl project, which is on Apple's dev site. I've added a flip view to the project and an info icon to the top right of each view/page. For some reason, only the first page is able to animate the flipping. Page 2 on still shows the flipped page but does not animate. To make sure it isn't anything special about page 1, I switch page 1 and 2 and that worked fine. Page 2 in position 1 animated while Page 1 in position 2 did not. Any ideas why this would happen or how I can trouble shoot it?

I did take a look at this thread, which seems to be the same issue: Flip View Iphone. However, My flipview is a UIViewController and so is the class with the info icon on it. In the other thread, they are using UIViews.

I did implement the showInfo code from the above thread. When on Page 2, I see no flip. Then I scroll over to Page 1 and see it has flipped. Not sure why it isn't staying with Page 2. When on Page 1, it doesn't animate the flip. The flipview just suddenly appears.


Solution

  • Do you have a containerView? Something that can be there so you can add and remove subviews from it? Animation can break if you have two viewControllers, one coming and one going, without a containerView. I use a rootViewController and animate all my pages to and from each other with the rootViewcontroller in the back. Here is my code for flipping, you'll probably have to do a little editing to make it work for you:

    (keep in mind that self is the rootViewcontroller, a viewcontroller with a blank view (color it so it matches your views))

    - (void)switchTwoViews:(UIViewController *)view1 otherView:(UIViewController *)view2
    {
        /*
         This method is called to switch views.
         It flips the displayed view from the main view to the flipside view and vice-versa.
         */
    
        UIViewController *coming = nil;
        UIViewController *going = nil;
        UIViewAnimationTransition transition;
    
        [view1.view setUserInteractionEnabled: NO];
        [view2.view setUserInteractionEnabled: NO];
        if (view1.view.superview == nil) {
            coming = view1;
            going = view2;
            transition = UIViewAnimationTransitionFlipFromLeft;
        }
        else {
            coming = view2;
            going = view1;
            transition = UIViewAnimationTransitionFlipFromRight;
        }
            // in some cases the following is needed to size the view
        //  coming.view.frame = [UIScreen mainScreen].applicationFrame;
    
        //  going.view.alpha = 1.0;     //uncomment these lines if we want fading of views
        //  coming.view.alpha = 0.0;
    
        NSArray *viewArray = [[NSArray alloc] initWithObjects:coming, going, nil];
        [coming viewWillAppear:YES];
        [going viewWillDisappear:YES];
        [UIView beginAnimations:@"View Flip" context:viewArray]; {
            [UIView setAnimationDuration:1.0];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(animationDidEnd:finished:context:)];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
            //      coming.view.alpha = 1.0;        //uncomment these lines if we want fading of views
            //      going.view.alpha = 0.0;
    
            [UIView setAnimationTransition:transition forView:self.view cache:YES];
            [self.view addSubview: coming.view];
        }
        [UIView commitAnimations];
    
    }
    
    - (void) animationDidEnd:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
    {
        NSArray *viewArray = context;
        [((UIViewController *)[viewArray objectAtIndex:1]).view removeFromSuperview];
        [[viewArray objectAtIndex:1] viewDidDisappear:YES];
        [[viewArray objectAtIndex:0] viewDidAppear:YES];
        [[[viewArray objectAtIndex:0] view] setUserInteractionEnabled: YES];
        [viewArray release];
    }