Custom UIViewController Transition? - ios

There are another custom UIViewController transition than the 4 natives iOS:
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
Any tips?

You could also try CATransition, for example, the code below shows a transition where a second view pushes your current view out to the right. (Assuming this code is placed in your main view controller).
UIView *appWindow = [self.view superview];
CATransition *animation = [CATransition animation];
[animation setDuration:0.3];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[appWindow addSubview:yourSecondViewController.view];
[self.view removeFromSuperview];
[[appWindow layer] addAnimation:animation forKey:#"showSecondViewController"];

Related

UILabel's layer doesn't update color while animating

I am animating a UILabel to 'evaporate' off the screen using CATransition.
I want the label text to turn green, and move off the screen.
The following code does 'evaporate' the label fine, but doesn't change its colour before animating:
CATransition *transition = [CATransition animation];
transition.beginTime = CACurrentMediaTime();
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromTop;
[self.displayLabel.layer addAnimation:transition forKey:#"evaporate"];
self.displayLabel.textColor = [self greenColor];
self.displayLabel.text = #" ";
Calling setNeedsDisplay on the label doesn't work.
Can't use a CABasicAnimation because the label text is changing.
What am I doing wrong and how do I fix it?
You basically want nested animations or, in a sense, looking for a completion block type thing.
The closest I can think of achieving this is by using the CATransition delegate
Example:
-(IBAction)btnTest:(UIButton *)sender
{
//[1] Animate text color change
CATransition *animation = [CATransition animation];
[animation setDelegate:self]; //important
[animation setRemovedOnCompletion:YES]; //better to remove the animation
[animation setBeginTime:CACurrentMediaTime()]; //not really needed
[animation setDuration:0.4];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[animation setType:kCATransitionFade];
//you can specify any key name or keep it nil. doesn't make a difference
[self.displayLabel.layer addAnimation:animation forKey:#"changeTextColor"];
[self.displayLabel setTextColor:[UIColor greenColor]];
}
#pragma mark - CATransition Delegate
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
//[2] Animate text sublayer
/*
The following CATransition should not set a delegate
otherwise this animation will loop continously
*/
CATransition *animation = [CATransition animation];
[animation setRemovedOnCompletion:YES]; //better to remove the animation
[animation setBeginTime:CACurrentMediaTime()]; //not really needed
[animation setDuration:0.4];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromTop];
//you can specify any key name or keep it nil. doesn't make a difference
[self.displayLabel.layer addAnimation:animation forKey:#"changeText"];
[self.displayLabel setText:#" "];
}

How to implement method: - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag

I have two blocks of code (described also in another topic)
- (void)AnimateImage:(NSString*)direction{
self.CurrentAnimal.image = [images objectAtIndex:image_nr];
CATransition *animation = [CATransition animation];
[animation setDuration:1.0];
[animation setType:kCATransitionPush];
if([direction isEqualToString:#"left"]){
[animation setSubtype:kCATransitionFromLeft];
}
else {
[animation setSubtype:kCATransitionFromRight];
}
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[[self.CurrentAnimal layer] addAnimation:animation forKey:nil];
}
And
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
//do what you need to do when animation ends...
}
I know that I need to "set the delegate property and implement the method" to catch animationDidStop event but I'm not able to get it working.
Question - How do I set up my .h and .m files so that I'm able to use this method to execute code when CATransition animation stops?
Set up the delegate:
CATransition *animation = [CATransition animation];
....
[animation setDelegate:self];
[animation setDuration:1.0];
[animation setType:kCATransitionPush];
You first have to add the Delegate to the CATransition object you created like that
CATransition *animation = [CATransition animation];
[animation setDelegate = self];
[animation setValue:animation.values.lastObject forKey:#"myAnimationKey"];
If you set a key for your animation, you can exactly check which animation ended. If you have just one animation, you don't need a specific key. If you set a key you can check which animation ended like that:
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if(anim == [animation.layer animationForKey:#"myAnimationKey"]){
}
}

UIView slide animation using blocks instead?

How can I get the following animation to work in blocks?
(I need the keyboard to dissappear in the completion handler of the transition animation)
- (void) showNewViewAnimatedSlideAscending:(BOOL)isAscending {
UIView * oldView = self.myView;
UIView * newView = self.myView;
UIView * superView = self.myView.superview;
[oldView removeFromSuperview];
[superView addSubview:newView];
// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.3];
[animation setType:kCATransitionPush];
if(isAscending) {
[animation setSubtype:kCATransitionFromRight];
} else {
[animation setSubtype:kCATransitionFromLeft];
}
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[superView layer] addAnimation:animation forKey:#"myTransitionAnimation"];
if(self.keyboardVisible) {
[self.view endEditing:YES];
}
}
p.s; I know, I'm removing and showing the same view. This view has received new data just before calling this method. Somehow the view displays correctly and only shows the new data in the 'new' view.

How can we animate the ipad view like a book

I want to animate the view in ipad like a book, view should be turned like a page from right to left and vice versa. Can anyone provide me any tutorial or example for this.
Thanks in advance.
You want to use the UIPageViewController
See e.g. this tutorial
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setDuration:1.0f];
animation.startProgress = 0.2;
animation.endProgress = 1;
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
[animation setType:#"pageCurl"];
[animation setSubtype:kCATransitionFromLeft];
[animation setFillMode: kCAFillModeBackwards];
[self.view.layer addAnimation:animation forKey:#"animation"];
for flipping the other way you make :
[animation setType:#"pageUnCurl"];
Hope this helps, good luck :)
check link
UIPageViewController

iOS: Animating Incorrect Orientation at rootViewController?

Context: I am trying to perform a custom animation from a normal UIViewController.view to a UISplitViewController.view. The animation should show from Left to Right.
I set self.window.rootViewController = viewController where viewController is a normal UIViewController.
Once the user swipe, the following gets called:
UIView *theWindow = [viewController.view superview];
[viewController.view removeFromSuperview];
[theWindow addSubview:self.splitViewController.view];
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToView1"];
When the device is in a portrait mode, everything went perfectly. However, when the device is in the landscape mode, the transition animation performs as if the device is still in the portrait mode. For example: Instead of coming in from the left, it comes in from the bottom. The orientation of both the views are completely correct. Only the transition is weird.
So, I've been playing around with your code (I did a best-guess reconstruction of how you have your project set up), and I was able to get the desired effect with the following code. I tried setting the frames and bounds of layers, of views, or sublayers, etc., and none of that worked. CATransform3D's, CATransform3DRotates, etc also weren't doing the trick. I also Googled for a solid hour. Either no one has had your issue, or no one has solved your issue. Regardless, this solution works, and unless some Core Animation guru can provide a better solution, you're welcome to this:
- (void)swipeGesture:(UISwipeGestureRecognizer *)sender {
UIView *theWindow = [self.viewController.view superview];
UIInterfaceOrientation interfaceOrientation = self.viewController.interfaceOrientation;
NSString *subtypeDirection;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
subtypeDirection = kCATransitionFromTop;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
subtypeDirection = kCATransitionFromBottom;
}
else {
subtypeDirection = kCATransitionFromLeft;
}
[self.viewController.view removeFromSuperview];
[theWindow addSubview:self.splitViewController.view];
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:subtypeDirection];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToView1"];
}

Resources