UIViewController custom transitions and state restoration - ios

I have a view controller that is using a custom transition, and it is working well. It takes advantage of the fact that when using UIModalPresentationCustom, the presenting view controller is not removed, and places the new view controller over it with transparency.
However, when using this in combination with state restoration, the presenting view controller IS removed. It would seem that because the view controller is presented without animations, the custom transition code is never called. Is there any way to have the custom transition activated, even when the view controller is not being animated?
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
// never called if we aren't animating
return self;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
return self;
}
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.25;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (toViewController == self) {
[transitionContext.containerView addSubview:fromViewController.view];
[transitionContext.containerView addSubview:toViewController.view];
self.maskView.alpha = 0.0f;
self.menuView.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, 286.0f);
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
[self.menuView updateFrame:^(CGRect *frame) {
frame->origin.y = self.view.bounds.size.height - frame->size.height;
}];
self.maskView.alpha = 0.75f;
} completion:^(BOOL finished) {
self.maskView.userInteractionEnabled = YES;
[transitionContext completeTransition:YES];
}];
} else {
[transitionContext.containerView addSubview:toViewController.view];
[transitionContext.containerView addSubview:fromViewController.view];
self.maskView.userInteractionEnabled = NO;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
[self.menuView updateFrame:^(CGRect *frame) {
frame->origin.y = self.view.bounds.size.height;
}];
self.maskView.alpha = 0.0f;
} completion:^(BOOL b){
[transitionContext completeTransition:YES];
[self.menuView updateFrame:^(CGRect *frame) {
frame->origin.y = self.view.bounds.size.height - frame->size.height;
}];
}];
}
}

I can't see how your presentingVC could be removed from hierarchy on state restoration, but what would helpful is to put the following in your app delegate:
- (UIViewController*)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder {
NSLog(#"Application restore state = %#", [identifierComponents componentsJoinedByString:#"."]);
return nil;
}
This way you can follow the state restoration logic if you do not implement custom state restoration classes and rely on Storyboard to do this for you. This may give a food for thoughts.

Related

Custom Modal transition not working - example

I am trying to get UIViewControllerContextTransitioning working.
What I want:
What I would like to have, is presenting modal view controller with custom animation and transparent background.
Wthat I did:
I created animator implementing UIViewControllerTransitioningDelegate,
Set for modal controller:
self.modalPresentationStyle = UIModalPresentationCustom;
self.transitioningDelegate = self;
What I achieved so far,
Modal controller animates presenting and dismissing view correctly, but after dismissing is finished, entire app becomes black. I used xCode tool to pick what's in window hierarchy, and there is nothing. My guess is, that I changed VC's superview when adding to context's container.
Animator
#implementation AlertAnimator
const static CGFloat kAnimationDuration = 1.2;
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController *to = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *from = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
if (self.transitionType == ModalAnimatedTransitioningTypePresent) {
[self animatePresentingInContext:transitionContext toVC:to fromVC:from];
} else if (self.transitionType == ModalAnimatedTransitioningTypeDismiss) {
[self animateDismissingInContext:transitionContext toVC:to fromVC:from];
}
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return kAnimationDuration;
}
- (void)animatePresentingInContext:(id<UIViewControllerContextTransitioning>)transitionContext toVC:(UIViewController *)toVC fromVC:(UIViewController *)fromVC {
CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC];
CGRect toVCRect = fromVCRect;
toVCRect.origin.y = toVCRect.size.height;
toVC.view.frame = toVCRect;
UIView *container = [transitionContext containerView];
[container addSubview:fromVC.view];
[container addSubview:toVC.view];
[UIView animateWithDuration:kAnimationDuration animations:^{
toVC.view.frame = fromVCRect;
} completion:^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
- (void)animateDismissingInContext:(id<UIViewControllerContextTransitioning>)transitionContext toVC:(UIViewController *)toVC fromVC:(UIViewController *)fromVC {
CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC];
fromVCRect.origin.y = fromVCRect.size.height;
UIView *container = [transitionContext containerView];
[container addSubview:toVC.view];
[container addSubview:fromVC.view];
[UIView animateWithDuration:kAnimationDuration animations:^{
fromVC.view.frame = fromVCRect;
} completion:^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
#end
Code example:
https://www.dropbox.com/s/oaghtgwvga4nxs4/Test.zip?dl=0
Question:
What I am doing wrong and why screen becomes black?
if this is a presenting animator you should not add the fromVC.view as a subview because its already there. This will cause it to bug out. Try it out and tell me what happens. If its a dismissal animator you shouldn't add the presentingViewController to the hierarchy either.

UIViewControllerContextTransitioning and UINavigationControllerDelegate

My code :
navigator.m
- (void)newPushPage:(UIViewController *)controller
{
[self pushViewController:controller animated:YES];
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
if (operation == UINavigationControllerOperationPush || operation == UINavigationControllerOperationPop)
{
self.animator = [Animator new];
return self.animator;
}
return nil;
}
animator.m
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.5;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
toViewController.view.alpha = 0;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
toViewController.view.alpha = 1;
} completion:^(BOOL finished) {
fromViewController.view.transform = CGAffineTransformIdentity;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
After the pushPage and the screen appears, there is a problem : are visible all the elements that hide in the code and I can see how elements disappear already seeing the screen. It looks unsightly . There is a way ?
inside animateTransition (id)transitionContext protocol push and pop transition should has to be handled separately as below.
//1.Settings for the fromVC ..
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
CGRect sourceRect = [transitionContext initialFrameForViewController:fromVC];
CGRect finalFrameForVC = [transitionContext finalFrameForViewController:toVC];
//2.Insert the toVC view.
if(pushCondition) {
UIView *container = [transitionContext containerView];
[container insertSubview:toVC.view aboveSubview:fromVC.view];
toVC.view.alpha = 0.5;
toVC.view =
} else if (popCondition ){
UIView *container = [transitionContext containerView];
toVC.view.frame = finalFrameForVC;
toVC.view.alpha = 0.5;
[container addSubview:toVC.view];
[container sendSubviewToBack:toVC.view];
UIView *snapShoot = [fromVC.view snapshotViewAfterScreenUpdates:false];
}
//3.Perform the animation.
[UIView animateWithDuration:1.0
delay:0.0
usingSpringWithDamping:1.0
initialSpringVelocity:6.0
options:UIViewAnimationOptionCurveLinear
animations:^{
//Setup the final parameters of views for push
toVC.view // update final view frame
toVC.view.alpha = 1.0;
//Setup the final parameters of views for pop
snapShoot.frame =
} completion:^(BOOL finished) {
//When the animation is completed call completeTransition with final push value
[snapShoot removeFromSuperview];
//When the animation is completed call completeTransition with final push value
toVC.view.alpha= 1.0;
[transitionContext completeTransition:YES];
}];
Create enum / flag property inside amimator and set it inside navigation controller delegate.
if (operation == UINavigationControllerOperationPush)
{
self.animator = [Animator new];
self.animator.pushPopAnimation = UINavigationControllerOperationPush;
return self.animator;
}
See the apple documentation of snapshotViewAfterScreenUpdates:(BOOL)afterUpdates method in code above.

Custom push view controller with slide down animation

I would like to custom push and pop a view controller use pull down/up animation like this:
I try to change the y position but it doesn't work (it doesn't show up animation at all).
[self.navigationController pushViewController:nextController animated:NO];
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[UIView animateWithDuration:0.6 animations:^{
self.view.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
} completion:nil];
Is there any suggestion?
P/s: I have to use push view controller in this case instead of presentViewController
Update:
I try to use UINavigationControllerDelegate like this:
PropertyViewController.h
#interface PropertyViewController : UIViewController <UINavigationControllerDelegate>{
}
PropertyViewController.m
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(#"willShowViewController");
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSLog(#"didShowViewController");
}
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
// THIS METHOD IS NOT CALLED AT ALL
NSLog(#"animationControllerForOperation");
TLTransitionAnimator *animator = [TLTransitionAnimator new];
animator.presenting = (operation == UINavigationControllerOperationPush);
animator.duration = 0.5;
return animator;
}
- (void)viewGallery{
// PUSH VIEW CONTROLLER
GalleryViewController* galleryController = [[GalleryViewController alloc] initWithNibName:#"GalleryViewController" bundle:nil];
galleryController.navigationController.delegate = self;
[self.navigationController pushViewController:galleryController animated:YES];
}
Update 2:
After fix the problem method not called with this line in PropertyViewController.m
self.navigationController.delegate = self;
I face with another problem.
The slide down animation on push does work, but the slide up doesn't .Here is my custom animation:
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
if (self.presenting) { // push
fromViewController.view.transform = CGAffineTransformIdentity;
toViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
}else{ // pop
fromViewController.view.transform = CGAffineTransformMakeTranslation(0, fromViewController.view.frame.size.height);
}
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
if (self.presenting) { // push
fromViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
toViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
} else { // pop
fromViewController.view.transform = CGAffineTransformMakeTranslation(0, 0);
}
} completion:^(BOOL finished) {
toViewController.view.transform = CGAffineTransformIdentity;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
The problem 2:
The pull down animation only works on the first time, from the second times, layout renders wrong, I have no idea why this can happen.
Where am I wrong here?
You should Implement UIViewControllerAnimatedTransitioning.
Make your FromViewController.m conform to UINavigationControllerDelegate. Other sample code out there tells you to conform to UIViewControllerTransitioningDelegate, but that's only if you're presenting the ToViewController.
#interface ViewController : UIViewController <UINavigationControllerDelegate>
Return your custom transition animator object in the delegate callback method in FromViewController:
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC {
TransitionAnimator *animator = [TransitionAnimator new];
animator.presenting = (operation == UINavigationControllerOperationPush);
return animator;
}
Create your custom TransitionAnimator animator class and paste these sample methods:
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
return 0.5f;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
if (self.presenting) {
toViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
}
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
if (self.presenting) {
toViewController.view.transform = CGAffineTransformIdentity;
} else {
fromViewController.view.transform = CGAffineTransformMakeTranslation(0, toViewController.view.frame.size.height);
}
} completion:^(BOOL finished) {
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
Most of the implementation is from here, I just edited the transition method to your needs: https://stackoverflow.com/a/25026102/2242359
I still strongly recommend to go over some or all these guides:
http://www.teehanlax.com/blog/custom-uiviewcontroller-transitions/
http://www.objc.io/issue-5/view-controller-transitions.html
http://objectivetoast.com/2014/03/17/custom-transitions-on-ios/
Try this :
UIViewController *yourViewController = [[UIViewController alloc]init];
[UIView beginAnimations: #"Showinfo"context: nil];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController: yourViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
[UIView commitAnimations];

iOS 7 Custom Transition: Will Not Animate

I am hoping you can help me I am trying to implement a Custom Transition in my app that will display a Custom Modal View Controller. I am able to get the View Controller to appear as expected, however, I cannot get the transition to animate. Any help with this or pointers in the right direction would be greatly appreciated.
Please see my code below:
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 1.0;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
CGRect sourceRect = [transitionContext initialFrameForViewController:fromVC];
fromVC.view.frame = sourceRect;
UIGraphicsBeginImageContextWithOptions(fromVC.view.frame.size, NO, 0);
[fromVC.view drawViewHierarchyInRect:fromVC.view.bounds afterScreenUpdates:YES];
UIImage *fromVCImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView *container = [transitionContext containerView];
[container setBackgroundColor:[UIColor colorWithPatternImage:fromVCImage]];
[container insertSubview:toVC.view belowSubview:fromVC.view];
CGRect fromVCFrame = fromVC.view.frame;
__block CGRect toVCFrame = toVC.view.frame;
toVCFrame.origin.y = fromVCFrame.origin.y + fromVCFrame.size.height;
[toVC.view setFrame:toVCFrame];
[toVC.view setAlpha:0.0];
[toVC.view setBackgroundColor:[UIColor colorWithPatternImage:fromVCImage]];
[transitionContext finalFrameForViewController:toVC];
//3.Perform the animation...............................
[UIView animateWithDuration:1.0
animations:^{
toVCFrame.origin.y = fromVCFrame.origin.y;
toVC.view.frame = toVCFrame;
[toVC.view setAlpha:1.0];
}
completion:^(BOOL finished) {
//When the animation is completed call completeTransition
[transitionContext completeTransition:YES];
}];
}
Adding Code from View Controller when presentViewController is called via a UITapGestureRecognizer
- (void) onTapGesture:(UITapGestureRecognizer *)recognizer
{
ImagesCollectionViewController *imagesCollectionView = [[Utils getStoryboardForDeviceWithIdentifier:MAIN_STORYBOARD] instantiateViewControllerWithIdentifier:IMAGES_VIEW];
imagesCollectionView.transitioningDelegate = self;
imagesCollectionView.modalTransitionStyle = UIModalPresentationCustom;
[self presentViewController:imagesCollectionView animated:YES completion:^{
}];
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
return self.contentSlideTransitionManager;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
return self.contentSlideTransitionManager;
}
I'm going to guess that this is your problem:
[container insertSubview:toVC.view belowSubview:fromVC.view];
If toVC.view is below fromVC.view, it is hidden behind it, so you are not seeing what it does.

UIViewControllerAnimatedTransitioning with a MPMoviePlayerViewController

I'm trying to do a custom presentation animation but the VC is an MPMoviePlayerViewController. I was following this tutorial:
When I present the MPMoviePlayerViewController, all is right.
But when I dismiss it, simply the method:
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
is not called, what I'm doing wrong?
This is the code:
- (IBAction)playButtonTouchedIn:(id)sender {
NSURL *url = [[NSBundle mainBundle]URLForResource:#"Learn to speak Spanish quickly with funny videos on youtube" withExtension:#"mp4"];
MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
player.transitioningDelegate = self;
player.modalTransitionStyle = UIModalPresentationCustom;
[player.moviePlayer setShouldAutoplay:NO];
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
[self presentViewController:player animated:YES completion:^{
[player.moviePlayer play];
player.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
}];
}
The UIViewControllerTransitioningDelegate methods:
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController: (UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
self.presenting = YES;
return self;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
self.presenting = NO;
return self;
}
The UIViewControllerAnimatedTransitioning Methods
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return 0.7;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
if (self.isPresenting) {
[fromViewController.view setUserInteractionEnabled:NO];
[transitionContext.containerView addSubview:fromViewController.view];
[transitionContext.containerView addSubview:toViewController.view];
CGRect startFrame = toViewController.view.frame;
toViewController.view.frame = (CGRect){CGPointMake(toViewController.view.frame.size.width, 0),startFrame.size};
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:0.5f initialSpringVelocity:4.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
toViewController.view.frame = startFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
[fromViewController.view setUserInteractionEnabled:YES];
}];
}
else{
[toViewController.view setUserInteractionEnabled:YES];
[transitionContext.containerView addSubview:toViewController.view];
[transitionContext.containerView addSubview:fromViewController.view];
CGRect finalFrame = (CGRect){CGPointMake(-fromViewController.view.frame.size.width, 0),fromViewController.view.frame.size};
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.frame = finalFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
I have tried with a view controller developed by me and it works great, so is should be the MPMoviePlayerViewController?
The way I was able to get it to animate properly on dismissal was to re-set the transitioningDelegate and modalTransitionStyle right before calling [presentingViewController dismissViewControllerAnimated:YES];
So instead of just
- (IBAction) closePressed:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:YES];
}
You would do
- (IBAction) closePressed:(id)sender {
MPCustomModalTransitionDelegate *delegate = [[MPCustomModalTransitionDelegate alloc]init];
self.transitioningDelegate = delegate;
self.modalTransitionStyle = UIModalPresentationCustom;
[self.presentingViewController dismissViewControllerAnimated:YES];
}
The obvious difference here is the addition of a new MPCustomModalTransitionDelegate class, which you would need to refactor out of your view controller.

Resources