2

I simply can't get this to work, I would think it was simple, but no luck.

- (void) animate {

    UIView *viewA = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 100.0f)];
    [viewA setBackgroundColor:[UIColor blueColor]];
    UIView *viewB = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 100.0f)];
    [viewB setBackgroundColor:[UIColor brownColor]];

    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 300.0f, 100.0f)];
    [container addSubview:viewA];

    [self.view addSubview:container];

    [UIView transitionWithView:container
                      duration:0.4
                       options:UIViewAnimationOptionTransitionFlipFromLeft
                    animations:^{
                        [[[container subviews] objectAtIndex:0] removeFromSuperview];
                        [container addSubview:viewB];
                    }
                    completion:^(BOOL finished){

                    }];


}

This is how the documentation recommends you do it, make a container, add/remove the two subviews you want to flip between.

I can't get it to work. It will just display viewA, then viewB, as if the animation part is skipped, but the block is carried out?

If I switch the container in the [UIView transitionWithView:container with self.view it flips the entire view (As suspected) but I can not get it to do this with 2 subviews of self.view.

Is there no way around this?

I am looking to do something like the iPad Photos app, where a picture will flip and scale to full screen.

I really hope someone could help me out here, thank you in advance.

RickiG
  • 11,380
  • 13
  • 81
  • 120

1 Answers1

-3

You can't put semicolons inside the block. If you want two things done in there, divide them with a ",". Also CurveEase would probably be nice., if you want it.

This should work.

[UIView transitionWithView:container
                  duration:0.4
                   options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionCurveEaseInOut
                animations:^{
                    [[[container subviews] objectAtIndex:0] removeFromSuperview],
                    [container addSubview:viewB];
                }
                completion:^(BOOL finished){

                }];

If it does not, consider making the views instance variables and make sure they're allocated before you try to animate them. If you want to animate on a button push, you can do it directly with sender too, if the whole view is a UIButton.

I hope it helps.

warrenm
  • 31,094
  • 6
  • 92
  • 116
Nicki
  • 984
  • 8
  • 7
  • Yes that is it! semicolons. This was not clear from the documents. Thanks Nicki! – RickiG Jan 30 '11 at 12:08
  • You're welcome. The first time I stumbled on that, I raged for quite a bit of time before I found out about the commas. Glad I could spare you the same anguish. :D – Nicki Feb 01 '11 at 09:29
  • 1
    Are you sure the semicolons were the issue? I'm using them at the moment in exactly the same method, and also in Apples example in the documentation they use semicolons, and everything works perfectly. – Pascal Mar 01 '11 at 18:46
  • Huh, weird. I can't remember where I found the bit about commas instead of semicolons, but it fixed it for me. Maybe they changed it? [View example](http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html#//apple_ref/doc/uid/TP40009503-CH6-SW2) uses semicolons. Weird. For me it wouldn't even build. – Nicki Mar 09 '11 at 15:28
  • Where did you get this from? Of course you can put semicolons inside the block. Please edit your post. – yakovlev Jan 10 '12 at 22:40
  • Practical experience. Semi-colons broke it, using commas made it work. – Nicki Jan 12 '12 at 11:36