iOS view transitions using blocks - iphone

I need to switch from 3 views of same viewcontroller. I am reaching it properly but I always get a fade transition. I do not understand how exactly works. Which parameter defines type of transition? setType or layer forKey:? I tried both but I always get same effect! Thank you
CATransition *trans =[CATransition animation];
trans.delegate = self;
[trans setDuration:2];
[trans setType:kCATransitionFromTop];
[trans setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
CALayer *layer = self.view.layer;
[layer addAnimation:trans forKey:kCATransitionMoveIn];
[self.view addSubview:vistaSocial];

To transition between multiple views you can simply use the +transitionFromView:toView:duration:options:completion: method of UIView. The options allow you to specify which type of transition you would like.
For example:
[UIView transitionFromView:view1 toView:view2 duration:0.35 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

Related

Is there an alternative way to perform this animation using animation blocks?

I'm currently performing a curl up animation by doing the following:
CATransition *animation = [CATransition animation];
animation.type = #"pageCurl";
animation.subtype = kCATransitionFromTop;
animation.fillMode = kCAFillModeForwards;
animation.duration = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[[self.view.window layer] addAnimation:animation forKey:nil];
This animation will simply perform a page curl animation but in the end you are left looking at the same view that you started out with. No REAL transition ever occurred.
Now using animation blocks I know you can do something to the effect of:
[UIView transitionFromView:self.view
toView:aNewView // a view to transition to
duration:1
options:UIViewAnimationTransitionCurlUp|UIViewAnimationOptionCurveEaseIn
completion:NULL];
However, using animation blocks you are transitioning to a new view, aNewView, which is different from the CATransition animation above which reuses the same view (no real transition ever occurs). The problem with the animation block is that you have to actually create the new view to transition to which is cumbersome in my case because the view is rather complicated and I'd rather not create a UIView subclass.
Is there a way to perform the above CATransition animation using animation blocks while getting around the difficulties of having to rebuild a view or create a custom subclass?
Ok - so I figured it out for anyone who is interested. It's actually super simple. You can simply use the method: transitionWithView:duration:options:animations:completion:
For example:
[UIView transitionWithView:self.view
duration:1
options:UIViewAnimationOptionTransitionCurlUp|UIViewAnimationCurveEaseIn
animations:^{ // do anything you want here }
completion:NULL];
That's it. This will show a transition animation back to the same view. You can make any changes you want to the new view (maybe display some new text, whatever...) in the animation block.
I hope this helps someone out down the line...

Switching views using Core Animation not working?

Currently I am having a issue switching views using Core Animation. I want to fade through black switching to my next view.
Right now it does not do anything besides lose touch events from my original view.
What am I doing wrong in the code below?
Edit1 Code:
- (void)changeView1ToView2 {
CABasicAnimation *fadeout= [CABasicAnimation animationWithKeyPath:#"opacity"];
[fadeout setDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:)];
[fadeout setToValue:[NSNumber numberWithFloat:0]];
[fadeout setDuration:0.5];
[[self.view layer] addAnimation:fadeout forKey:#"alpha"];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
[self.view addSubview:self.view2.view];
self.view2.view.frame = [UIScreen mainScreen].bounds;
[self.view2.view setAlpha:0];
CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:#"opacity"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[self.view2.view layer] addAnimation:fadein forKey:#"alpha"];
}
Ok I added self, look at my new code above. view2 is a UIViewController, thats why I am doing .view after it. The app is only going to be available on iOS 5 or up so thats not a problem. But what I am trying to achieve is switching views using Core Animation, and have each UIViewController manage their own views. I am just switching views using Core Animation instead of usual means.
If you're looking to have only one root view on screen at one time (and by the looks of that call to [UIScreen mainScreen].bounds, you are), then I'd suggest swapping the views at the UIWindow level. Without animations, this would look something like this:
// Assuming UIViewControllers called view1 and view2 as members of some
// (non-UIViewController) controller class (self, in this case)
//and that view1.view is in the application's window's subviews collection
[self.view1.view removeFromSuperview];
[[UIApplication sharedApplication].delegate.window addSubview:self.view2.view];
In terms of not seeing the views actually swap, you need to ensure that your animation preserves the changes you make during the animation. Specifically, you need to set the following:
myAnimation.fillMode = kCAFillModeForwards;
myAnimation.removedOnCompletion = NO;
Your methods can then be adjusted to take all this into account. For example, your animationDidStop:finished: method might look like this:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[self.view1.view removeFromSuperview];
[self.view2.view setAlpha:0];
[[UIApplication sharedApplication].delegate.window addSubview:self.view2.view];
CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:#"opacity"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
fadein.fillMode = kCAFillModeForwards;
fadein.removedOnCompletion = NO;
[[self.view2.view layer] addAnimation:fadein forKey:#"alpha"];
}
You may need to muck around with it a bit to ensure that everything is firing correctly.

view animations

I'm trying to add a sub view with Core-Animation using the attached code.
First time it happens as expected, but after that there's a flash of white in the place of the sub-view before it's fully pushed.
// Add the picker
viewToPush.frame = CGRectMake(0,185,320, 258);
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromTop];
[self.view addSubview:viewToPush];
[viewToPush.layer addAnimation:animation forKey:nil];
btw, in order to remove the subView I just use
[viewToRemove removeFromSuperview];
10x
Why do you add the animation to the viewToPush's layer, not the super view's? I suspect the layer of viewToPush is not stable as you remove the view from the super view. Do you have many views that can act as viewToPush or viewToRemove so they have to be dynamically allocated? Otherwise I would just change their hidden properties to implement such animations.

Giving animations when we set the visibility of a UIDatePicker

Hai all,
in my iphone application, when i click a UIButton it will show a UIDatePicker (using setVisible:YES) ,is there any way to animate the DatePicker appearance,(now when the user taps it will suddenly appear in the UI)
thanks in advance
Yes , it is possible to have an animation when you call your picker to set visible.
Initially your picker is hidden. When UIButton is pressed You just call below method (animatePicker).
In below method there is just hidden set to false for pickerview but with CAanimation.
-(void)animatePicker
{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
// Set the type and if appropriate direction of the transition,
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromTop];
// Set the duration and timing function of the transtion -- duration is passed in as a parameter, use ease in/ease out as the timing function
[animation setDuration:0.4];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[[PickerView layer] addAnimation:animation forKey:#"transitionViewAnimation"];
PickerView.hidden = FALSE;
[[PickerView layer] removeAnimationForKey:#"transitionViewAnimation"];
animation = nil;
}
Note: Please include Quartz framework(QuartzCore.framework) and import its header file( QuartzCore/QuartzCore.h) to your controller.
A good idea is to bring it up like any other keyboard. Start it offscreen. Start an animation, set it to a position onscreen, then commit the animation. Otherwise you can do any other crazy view animation you want to do. Fade in, other slide animations, expand into view perhaps?

Flip transition in iPhone

I am facing problems in flipping views in iPhone.
I have two views in appDelegate. I want to flip them once user clicks on a button.
I have the following code:
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
[transition #"twist"];
[transition setSubtype:#"fromRight"];
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[transition setFillMode:#"extended"];
[[window layer] addAnimation:transition forKey:nil];
[window addSubview:self.s.view];
[CATransaction commit];
But this is not working. Do anybody knows a better way to flip the views on window side.
What I am doing is calling the method from appDelegate in the respective viewControllers to flip the views.
If you're using the 3.0 SDK and all you want is a simple flip transition (ala the Weather app) then you don't need to go down to CATransition. The higher-level UIView animation transitions will do what you want but with 3.0 there is an even easier way: simply present your new view as a modal view controller and set the modal transition style to flip. From within the first controller:
UIViewController *controllerForSecondView = ..;
controllerForSecondView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controllerForSecondView animated:YES];
Flip back again by using dismissModalViewController.
Documentation Reference
#Luke - thanks, this sample helped me...1 correction though (based on UIViewController.h)
UIViewController *controllerForSecondView = ..;
controllerForSecondView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controllerForSecondView animated:YES];
From the header file comments:
// Defines the transition style that will be used for this view controller when it is presented modally. Set this property on the view controller to be presented, not the presenter.
// Defaults to UIModalTransitionStyleSlideVertical.
#property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle
See The Elements sample code. Particularly AtomicElementViewController -flipCurrentView.