Problems moving animation to iOS4 blocks - iphone

I have a working view animation, that curls up a container view, while the containerview.subviews changes. (before the animation a UITableView will be shown, after it is a custom view, name keypadView)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:containerView
cache:YES];
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
[UIView commitAnimations];
Now I want to rewrite this code for the iOS4 block-based api, as I want to use the completion block. I wrote this:
[UIView transitionWithView:containerView
duration:.75
options:UIViewAnimationTransitionCurlUp
animations:^{
NSLog(#"Hey Ho");
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
}
completion:NULL];
The views switch — but not animated.
what is wrong with my code?
Edit
completion: ^(BOOL completed){
NSLog(#"completed %d", completed);
}
doesn't help, as NULL is an accepted value, according to the docs

do:
options:UIViewAnimationOptionTransitionCurlUp
instead of:
options:UIViewAnimationTransitionCurlUp
That is why your code works now :).

The sample in the UIView class reference may be wrong - or maybe there's a bug with adding and removing views in the animations block object, but the only way I've been able to get it to work is as follows:
[secondView removeFromSuperview];
[containerView addSubview:keypadView];
[UIView transitionWithView:containerView
duration:.75
options:UIViewAnimationOptionTransitionCurlUp
animations:^{}
completion:^(BOOL finished) {
NSLog(#"finished %d", finished);
}];

Did you leave [UIView beginAnimations:nil context:nil]; above your new block?

Is the completion block always NULL? Try putting an NSLog statement in there or something. I don't know if NULL blocks would mess it up.

Related

How animate a view in with a flip animation

This is my current code, but it seems to just be flipping the view - I want it to flip-in i.e. flip for not seeing it to seeing it. I am doing this in the viewdidload method:
[UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionOverrideInheritedDuration animations:^{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.alertView cache:YES];
}completion:^(BOOL finished)
{
}];
Use the transitionFromView:toView:duration:options:completion: method with a full transpatent view as 'FromView' and your alertView as toView

UIView animation is leaking

In my viewcontroller i am using animation for changing the frame of UIButton and UIView, going from portrait to landscape user can see views growing but problem is animation is leaking everywhere and showing everything coming from different sides.
Here is the code
[UIView beginAnimations:Nil context:Nil];
[UIView setanimationDuration:1];
[view1 setFrame:CGRectMake(100,100,200,300)];
[UIView commitAnimations];
Thanks
Please elaborate more on what "leaking everywhere" means, and does it make a difference if you use block animation:
[UIView animateWithDuration:1.0 delay:0.0 options:nil animations:^{
[view1 setFrame:CGRectMake(100,100,200,300)];
}completion:^(BOOL done){
if (done) {
NSLog(#"animation complete");
}
}];
According to Apple's documentation:
In iOS 4 and later, use the block-based animation methods.
(Recommended)

Change between UIViewcontroller.view subviews with animation

I have one view controller which contains two views (redView, blueView). The views are smaller than the main view.
I want to change from redView to blueView with animation. If use this none animation happens.
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
forView:redView cache:YES]; // !!! I WILL CHANGE THIS
[self.view addSubview:blueView];
[redView removeFromSuperview];
[UIView commitAnimations];
In case I change the code to this, then the animation is ok but the whole mainView animates, something that i do not want to happen. I want only the subViews to flip. I should note that the 2 subviews are in the same position. (frame) Any ideas?
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[self.view addSubview:blueView];
[redView removeFromSuperview];
[UIView commitAnimations];
Use Block animation instead of simple animation, from apple about simple animation "Use of the methods in this section is discouraged in iOS 4 and later"
Animation With Block's
[UIView transitionWithView:containerView
duration:1.25
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^
{
[redView removeFromSuperview];
[containerView addSubview:blueView];
}
completion:NULL];
containerView : is main view that will be animated.
Also add to your containerView redView,
that all :)
for more info look at
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
Solution Found:
I use a tempView for which i do the animation. Onto the tempView I add view1 and view2.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:tempView cache:YES];
[tempView addSubview:view2];
[view1 removeFromSuperview];
[UIView commitAnimations];
Extra: For a better animation you can use view1.visible = YES , NO instead of addSubview, removeFromSuperView, but they will be allocated all the time
In your first example it looks like you are deleting the redView before it can animate, try deleting it after the animation.
Code:
[UIView setAnimationDidStopSelector:#selector(removeRedViewFromSuperview)];

Block transition with more than one view

up until now I used the begin/commit method to switch between views. Going this way it was easily possible to "combine" two or more views to be inserted on top at the same time. In my case it's a content2.view with a border2.view over it. The animation looked like it is one view with the content in a frame.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:speed];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[window addSubview:content2.view];
[window addSubview:border2.view];
[UIView commitAnimations];
content1.view = nil;
Now I wanted to convert the animations into blocks. It also works, but not I can't figure out how I can "merge" two views. Is there a way?
[UIView transitionFromView:content1.view
toView:content2.view
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromRight
completion:NULL];
You have to use the method:
transitionWithView:duration:options:animations:completion:
transitionFromView:toView is a convenience method for the most general case of needing to transition from one view to another view.
In this case, your code should be
[UIView transitionWithView:window
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{ [window addSubview:content2.view];
[window addSubview:border2.view]; }
completion:^(BOOL completed){ [content1.view removeFromSuperview];
content1.view = nil; }

Trouble with UIView Animations - performing the same animation more than once doesn't work

I need to flip a card and then flip it back. I have written code to do that with no trouble. The trouble comes because users can flip this card back and forth as many times as they want, and it seems that the effect is cumulative. On the second try, the card spins like a top. I'm not completely surprised, as there only seems to be a way to add animation, not clear one from the view. Is there a way to 're-set' a UIView of any animations I had previously committed? Is there a way to capture it and re-use it without committing a new one? Or am I missing something obvious? This is my code:
if (self.flipped) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self
cache:YES];
[imageView removeFromSuperview];
[UIView commitAnimations];
self.flipped = NO;
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self
cache:YES];
[self addSubview:imageView];
[UIView commitAnimations];
self.flipped = YES;
}
Thanks for reading.
To cancel all ongoing animations:
#import <QuartzCore/QuartzCore.h>
...
[view.layer removeAllAnimations];
To start a new animation that kills existing animations, if any, and starts the movement from wherever the view currently is — which I think is what you want:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
... and the rest of it ...
[UIView commitAnimations];
Hopefully one of those will solve your problem.
Whatever your "self" is, put it in a containing view called foo, and then change your setAnimation... line to this:
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:foo
cache:YES];