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; }
Related
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
I am using such code to present a new view, I don't it's good or not. Currently the default animation is show the view from bottom up, but i want it to animate from right to left(fly in), is it possible to change the default animation type and how? Thanks.
[self presentModalViewController:navController animated:animated];
You can disable the slide-up animation like this:
[self presentModalViewController:navController animated:NO];
Then you can provide your own animation code:
navController.view.frame = CGRectMake(320, 0, navController.view.frame.size.width, navController.view.frame.size.height);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
navController.view.frame = CGRectMake(0, 0, navController.view.frame.size.width, navController.view.frame.size.height);
[UIView commitAnimations];
This example gives you the "Fly-in" from right with a smooth speed-curve.
Another way is using the built in slide-in from right with navigationcontroller:
[self.navigationController pushViewController:navController animated:YES];
In this one, your top-viewcontroller needs to be a UINavigationController and it's rootcontroller needs to be your viewcontroller. Then you can push other viewcontrollers.
I have done this and its working for me try ths:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.View2.superview cache:YES];
[self.View2 removeFromSuperview];
[self performSelector:#selector(replaceViews2) withObject:nil afterDelay:0.6];
[UIView commitAnimations];
I wrote a basic two-view application that switches from one view to another every time I press a button.
But for some reason when I run it on simulator, both views are always few pixels above the MainWindow.xib view, always being on top of it. And what's strange is that there is no animation when I switch between Views.
What is the problem???
This is what I have in my AppDelegate.m
-(void)switchView:(UIView *)view1 toView:(UIView *)view2{
[UIView beginAnimations:#"Animation" context:nil];
[UIView setAnimationDuration:1.75];
[UIView setAnimationTransition:UIViewAnimationOptionTransitionFlipFromLeft forView:self.window cache:YES];
[view1 removeFromSuperview];
[window addSubview:view2];
[UIView commitAnimations];
}
Here are two very nice examples:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/13427-uiview-slide-transition.html
http://www.dizzey.com/development/ios/simple-uiview-transitions-animation-using-blocks-in-ios-4/
Or this one:
iOS 4.2: Flip image using block animations
Hi Try This,
-(void)switchView:(UIView *)view1 toView:(UIView *)view2
{
view2.frame = self.window.bounds;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2 cache:NO];
[view1 removeFromSuperview];
[window addSubview:view2];
[UIView commitAnimations];
}
Also try this
[UIView transitionWithView:view2 duration:0.5
options:UIViewAnimationTransitionFlipFromLeft //change to whatever animation you like
animations:^ { [window addSubview:view2]; }
completion:nil];
Can you post some code regarding this .Because if you click on Segment change
it will change the Views.IF you take two views on same interface Builder.
Try out this
IBOutlet UIView *viewA;
IBOutlet UIView *viewB;
in .m file
-(IBAction)SegmentChange
{
if(segment.selectedSegmentIndex==0)
{
viewA.hidden=NO;
viewB.hidden=YES;
}
else if(segment.selectedSegmentIndex==1)
{
[self.View addSubview:viewB];
viewA.hidden=YES;
viewB.hidden=NO;
}
}
i am working with page curling effect .on click of a button i was able to transit the page(i.e between the UIViews).the following code depicts the same
UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
if ([sender tag] == 1) {
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:placeholder cache:YES];
}
else {
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:placeholder cache:YES];
}
if (view1OnTop) {
[view1 removeFromSuperview];
[placeholder addSubview:view2];
}
else {
[view2 removeFromSuperview];
[placeholder addSubview:view1];
}
[UIView commitAnimations];
view1OnTop = !view1OnTop;
with this i was able to curl between UIViews ,but my question is , will i be able to apply this kind of transition between two or more classes???
thanks in advance
Not everything is animatable. Only parts of UIKit are. So if you mean to ask if any subclass of NSObject can be animated then no they can't. If you mean to ask if whether subclasses of UIView can be animated then the answer would be yes. They can be different subclasses too. While this is possible, it doesn't mean it will give us the right results. They might end up looking pretty weird. You might not want to do that.
Layers are animatable too.
However it all depends on what you mean by classes.
Animating to a new view controller
Say you want to alter the way you shift between view controllers, you can use transitionWithView:duration:.. class method of UIView. An example,
SecondViewController * viewController = [[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease];
[UIView transitionWithView:self.view.window
duration:1.0f
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self.navigationController pushViewController:viewController animated:NO];
}
completion:NULL];
This will use the curl up transition when pushing the new view controller.
For iOS versions older than 4.0
Since they don't support block based animation APIs, you will have to do this,
[UIView beginAnimations:#"Curl up" context:NULL];
[UIView setAnimationDuration:1.0f];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view.window cache:YES];
[self.navigationController pushViewController:viewController animated:NO];
[UIView commitAnimations];
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.