iPhone slide animation when view is loaded - iphone

How to perform a sliding animation when a view is being loaded in an iPhone application?
I have a ViewController in which I have subview. I want that the subview be loaded with an animation, sliding from the left to the right.
I got this code so far:
-(void) showAnimation{
[self.view addSubview:geopointView]
geopointView.frame = CGRectMake(20,150,550,200)// somewhere offscreen, in the direction you want it to appear from
[UIView animateWithDuration:10.0
animations:^{
geopointView.frame = CGRectMake(20,150,550,200)// its final location
}];
}

why not?
-(void) showAnimation{
[self.view addSubview:geopointView]
geopointView.frame = CGRectMake(-200,150,550,200)// just set a different frame
[UIView animateWithDuration:10.0
delay:0.0
options:UIViewAnimationCurveEaseOut
animations:^{
geopointView.frame = CGRectMake(20,150,550,200)// its final location
}
completion:nil];
}

Related

how to trigger uiview animation to wok again

i have an animation that works in the main menu screen of an iPad application.
when select some menu item, the application will push the new view the navigation controller.
the problem happens when i want to restart the animation again in the moment of when i push the back button and return to the main menu again.
i tried to put the animation code in these methods :
-(void) viewDidLoad
-(void) viewDidAppear
but i can't get them to work.
what i am trying to do is animation the company's logo in the background .
my code is :
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:15.0];
[UIView setAnimationRepeatCount:20.0f];
[UIView setAnimationRepeatAutoreverses:YES];
CGPoint pos = large_bright.center;
pos.x = 400.0f;
large_bright.center = pos;
CGPoint pos2 = large_dim.center;
pos2.x = -10.0;
large_dim.center = pos2;
[UIView commitAnimations];
you should call
(void)viewWillAppear:(BOOL)animated;
Try:
[UIView animateWithDuration:1.0 // set here how fast u want it to animate
animations:^{
// add your needed animation here
}
completion:^(BOOL finished) {
// do some stuff when its done if you need to
}
];
Call some other function from viewDidLoad and viewDidAppear.and then add your animation code in that function.
-(void) viewDidLoad
{
[self add_animation];
}
-(void)animation
{
//your animation code
}

Animate a UIView change where the UIView is totally rebuilt

My UIViewController gets the view it is controlling to completely rebuild itself with something like this:
[self.view rebuildYourself:bIsLandscape]; // this line is in the ViewController
The view itself then contains the following method:
-(void)rebuildYourself:(BOOL)isLandscape
{
NSArray *viewsToRemove = [self subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
[self addControls]; // adds lots of views
[self layoutControlsWithOrientation:isLandscape]; // frames the views
}
I would like to animate the entire transition. I have tried this:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
[self.view rebuildYourself:bIsLandscape];
}
completion:^(BOOL finished){
}];
but the animation ignores the options value of UIViewAnimationOptionTransitionCurlUp and flows in from the top left corner every time.
and I have tried this:
[UIView beginAnimations:#"kkk" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationDuration:1.0];
[self.view rebuildYourself:bIsLandscape];
[UIView commitAnimations];
but in this case the transition does curl up nicely but the underlying view is blank until the transition has finished, and then the new view suddenly 'pops' into view, which spoils the effect of the transition.
Can anyone tell me the right/best way to animate a transition where the view rebuilds itself completely?
Thanks
Make two views and put the old view overtop of the new re-built view. Then 'Curl-up' the old view so that the new view is showing and remove the old view from memory.

NAvigationbar jumping when loading view

I have implemented a container view controller (ios5) and now Im trying to implement custom animations/transitions for switching the view controllers.
But I have an issue when implementing any animation that involves a UINavigationController, the result is not as expected. For instance if I perform a flip from left animation when showing the controller with the navigation bar, the position for the navigation bar stays 44pxs below the original position and after the animation is performed the navigation bar will move smoothly to the original position which is really ugly...
This happens with every animation I perform, so I guess it has to be related only with the navigationcontroller.
This is the code I use for the flip transition
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:UIViewAnimationOptionTransitionNone
animations:^{
[UIView beginAnimations:#"animation2" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration: 0.7];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:NO];
[UIView commitAnimations];
}
completion:^(BOOL finished) {
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
}];
I also tried the following
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
}
completion:^(BOOL finished) {
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
}];
Set the frame of navigation bar according iphone or ipad's and its orientation like this for portrait in iphone
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, 320, 44)]; //for iphone simarly for ipad and its orientation
You need to change your navigation bar frame to its actual position from status bar ie plus 20
I have similar problem with NavigationController and Container controller. Actually it was the toolbar at the bottom of window. My solution is to hide the toolbar first, then show toolbar with animation after the main transition completed. It looks like part of the whole animation.
Here is how I hide and show toolbar:
[toNavController setToolbarHidden:YES];
[self transitionFromViewController:fromViewController
toViewController:toNavController
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
}
completion:^(BOOL finished) {
[toNavController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
[toNavController setToolbarHidden:NO animated:YES];
}];
In viewDidLoad set frame.origin.y to 1. You still have a jumping but with 1 pixel.

switch view with animation UIViewAnimationTransitionCurlUp

I am working on an iPad app which has a small popup view in front of the background main view.
The popup view displays one photo and its related notes etc. The main view is a bunch of thumbnails
I want to implement actions, like swipe left/right, to replace the popup view with a new popup view, with animation.
// popView is the existing pop up view to be replaced
MyPopupViewController *newPopView = [[MyPopupViewController alloc] initWithData:...];
[UIView animateWithDuration:0.4
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations: ^{
[self.view addSubView: newPopView.view];
[popView removeFromSuperView];
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp
forView:popView cache:YES];
} completion:^(BOOL finished) { popView = newPopView }
];
[newPopView release];
Well, this doesn't work. I can't remove the old view in animation block otherwise the animation won't fire. I can't remove it in the completion block either, because during the animation, the old image will still be visible under itself.
I've spent quite some time playing with the sequences but just can't get it to work. Please help.
Thanks in advance.
Leo
I finally fixed this by setting the old view hidden in animation block. The animation will reveal the new view during animation.
Here is how I did: it works well in landscape an curls from bottom to top
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionTransitionCurlUp animations:^()
{
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp
forView:self.welcomScreen cache:YES];
[self.welcomScreen setHidden:YES];
}completion:^(BOOL finished)
{
[self.welcomScreen removeFromSuperview];
}];

Flip View Iphone

please consider the code below, and tell me what I'm doing wrong.
I want to flip between two UIViews.
Somehow, when I flip away from the initial view, I just get the flipped view, without animation. When I flip back, the animation shows just fine.
The flips are triggered from buttons on the views themselves.
- (IBAction)showMoreInfo:(id)sender
{
UIView *moreInfo = self.flipView;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
UIView *parent = self.view.superview;
[self.view removeFromSuperview];
[parent addSubview:moreInfo];
[UIView commitAnimations];
}
- (IBAction)showLessInfo:(id)sender
{
UIView *lessInfo = self.view;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.flipView cache:YES];
UIView *parent = self.flipView.superview;
[self.flipView removeFromSuperview];
[parent addSubview:lessInfo];
[UIView commitAnimations];
}
It's probably because you aren't using a container view as the transition view. Refer to the documentation on setAnimationTransition:forView:cache:
If you want to change the appearance of a view during a transition—for example, flip from one view to another—then use a container view, an instance of UIView, as follows:
Begin an animation block.
Set the transition on the container view.
Remove the subview from the container view.
Add the new subview to the container view.
Commit the animation block.
Try using self.view.superview in the animation transition view of the showMoreInfo:
The reason the showLessInfo: method works is you are using a container view.
Can you use your MainWindow (UIWindow) as the container view as UIWindow inherence from UIView?
Also iPhone 3.0 introduced the flip transaction via the presentModalViewController method:
CustomViewController *vc = [[CustomViewController alloc]
initWithNibName:#"CustomViewController" bundle:nil];
vc.delegate = self;
// The magic statement. This will flip from right to left.
// present the modal view controller then when you dismissModalViewController
// it will transition flip from left to right. Simple and elegant.
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:vc animated:YES];
[vc release];
After iOS 4.0, you can flip between views with this:
[UIView transitionFromView:sourceView toView:destinationView duration:0.35f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
NSLog(#"I just flipped!");
}];
As Jason mentioned, you'll need this to occur within a container view.