Fade UIView and Switch UIView - objective-c

This is how I switch from one UIView to another UIView: self.view = MyView;
How would I fade out a view to my MySecondView?

You can use UIView's build in transition code to cross dissolve between them.
[UIView transitionFromView:self.view toView:MySecondView duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) {
// What to do when its finished.
}];
You can also use the regular animation options with something like this. I haven't tested this code as I'm not in front of Xcode but I use it all the time for labels and things without default animations.
[UIView animateWithDuration:0.25 animations:^{
[self.view setAlpha:0.0];
self.view = mySecondView;
[self.view setAlpha:1.0];
}];

Please note that it's not a good idea to do things like self.view = MyView to change screens. By the time you get to a few screens, your viewController will be filled with spaghetti code. You should consider presenting new view controllers that manage their views. One way you can do fade is as follows:
Fade the current view to black (with animation)
In the view controller that you are going to push use viewWillAppear to fade the view to black as well
Push/Present the view controller without animations.
Now use the viewDidAppear method of the newly presented view controller to fade in the view (with animation).

Related

Terminate a UIView transition animation

I am slowly animating a UIImageView from one image to another. Like so:
[UIView transitionWithView:self.backgroundView duration:30 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.backgroundView.image = endImage;
} completion:nil];
Is there some way to terminate the animation partway through, in response to an event?
A certain Tim Oliver tells me this is how you do it:
[self.backgroundView.layer removeAllAnimations];
You can also retrieve the frame the animation ended on using self.layer.presentationLayer.frame, if you need to freeze the view in the partially animated state.

Hiding an animated modal UIView?

I need to present and then dismiss a UITableView modally. It should animate in from top to button. But it should not cover the whole screen, it will slide in from under navigation bar and stop before it reaches the tabbar.
So i can't use the presentViewController:animated:completion: for its view controller.
I need to manipulate it as part of a view hierarchy. The question is, where should this table view be when it is not visible. Where will I animate it from?
You could put the UITableView inside a UIView and use animateWithDuration. For example (using some made up dimensions):
[subMenuView setFrame:CGRectMake(5,-400, 310, 400)];
[UIView animateWithDuration:0.8f
delay:0.0f
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
[subMenuView setFrame:CGRectMake(5,0,310,400)];
}
completion:^ (BOOL finished)
{
if (finished)
{
//animation has finished, you can do something here, even another nested animation
}
}];
If you want to then slide it back up, do the opposite...

Smoother transition from one UIView to another UIView

I have a UIViewController which has multiple subviews. Each subview is a UIView subclass, and I want to switch between views by tapping the toolbar buttons. I did this by using the animation blocks:
Example:
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[StoreView removeFromSuperview];
[self.view addSubview:HomeView];
}
completion:NULL];
Everything is working fine actually. The problem is the transition is not really smooth. For example, the HomeView has five buttons scattered (as part of the design), and whenever I switch from one view to HomeView, these buttons will come from a corner and rearrange itself after the transition, which is not exactly beautiful to look at.
So how will I make these buttons stay in place?
When doing animations with complex subviews you can sometimes get undesirable results as you are experiencing. Not only can some oddities appear, but they are sometimes costly depending on the complexity of the view structure. One suggestion I would make is instead of animating the complex views themselves, you could render the views to a graphics context and animate the resulting image in a UIImageView, using sleight-of-hand to make it appear that you are animating the view hierarchy. In this effect, you avoid doing what amounts to a complex transform on the HomeView and StoreView and instead do simple flip with UIImageView instances. Consider the following example code:
UIImageView *storeImage = // pointer to the image rendered to a graphics context
UIImageView *homeImage = // pointer to the image rendered to a graphics context
[self.view addSubview:storeImage];
[storeView removeFromSuperview];
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[storeImage removeFromSuperview];
[self.view addSubview:homeImage];
}
completion:^(BOOL finished) {
[self.view addSubview:homeView];
[homeImage removeFromSuperview];
}];

Flip vertical UIWebview to be shown when view did load

I was searching information to show a uiwebview using a flip horizontal effect like modal transition one in viewDidLoad event, but I wasn't successful, even in apple documents. Can anybody help me with that?
Many thanks
the transition to animate webView with the view add your webView like this.
[UIView transitionWithView:poster.view duration:3
options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[self.view addSubview:yourWebView];
}
completion:NULL];
The animation you want to add can be done on the UIView. So you should addSubView the UIWebview to the UIView and then do the animation on the UIView.

How to apply flip Horizontal transition to subview without effecting to superview

I have a superview and I've added a subview on superview and added a UIbutton on the subview.My requirement is when I click on UIbutton the flipHorizontal Transition, How will this be applied to subview(Nothing but UIbutton) without effecting to superview(means superview is stable one)?
[UIView transitionFromView:view1 toView:view2
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:^(BOOL finished){
/* do something on animation completion */
}];
Make sure you replace view1 with the name of your button you want to flip, and view2 with the view you want the button to be replaced with. You can just put your Button for both of them so it will flip around with no side effects if you like.