Is it impossible to flip subviews of a viewControllers.view with “transitionWithView” using a block
-
11-10-2019 - |
Question
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.
Solution
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.