I am using UIViewControllerTransitioningDelegate to build custom transitions between two view controllers (from a MKMapView) to a custom Camera built on (AVFoundation). Everything goes well until I call the presentViewController and the phone seems to hang for about 1 second (when I log everything out). This even seems to happen when I am transitioning to a much simpler view (I have a view controller that only displays a UITextview and even with that there appears to be about a .4 - .5 second delay before the transition is actually called).
This is currently how I am calling the transition
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
CameraViewController *cvc2 = [sb instantiateViewControllerWithIdentifier:#"Camera"];
cvc2.modalPresentationStyle = UIModalPresentationFullScreen; // Needed for custom animations to work
cvc2.transitioningDelegate = self; //Conforms to the UIViewControllerTransitioningDelegate protocol
[self presentViewController:cvc2 animated:YES completion:nil];
});
Here is my animateTransition method for that call. Very straight forward and currently the view that is presenting this only has a MkMapView on it (no additional views or methods).
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
if (self.type == MapAnimationTypePresent) {//From map to another view
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// Amazing category for iOS 7 compatibility found here - http://stackoverflow.com/a/25193675/2939977
UIView *toView = [toViewController viewForTransitionContext:transitionContext];
UIView *fromView = [fromViewController viewForTransitionContext:transitionContext];
toView.frame = self.view.frame;
fromView.frame = self.view.frame;
//Add 'to' view to the hierarchy
toView.alpha = 0;
[containerView insertSubview:toView aboveSubview:fromView];
[UIView animateWithDuration:.5 animations:^{
toView.alpha = 1;
}completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
Any help is greatly appreciated.
Related
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.
I am using custom view controller transitions, UIViewControllerAnimatedTransitioning, to present and dismiss a view controller.
The presenting animation works fine, but when I run the dismiss animation, once I call completeTransition: the containerView gets removed.
I'm not sure what is going on, here is the transition code:
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
containerView.backgroundColor = [UIColor blackColor];
if (self.reverse) {
[containerView addSubview:toViewController.view];
[containerView addSubview:fromViewController.view];
} else {
[containerView addSubview:fromViewController.view];
[containerView addSubview:toViewController.view];
}
if (! self.reverse) { // Forward
toViewController.view.frame = CGRectMake(-containerView.frame.size.width, 0, containerView.frame.size.width, containerView.frame.size.height);
} else {
fromViewController.view.frame = CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height);
}
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{
if (self.reverse) {
fromViewController.view.frame = CGRectMake(-containerView.frame.size.width, 0, containerView.frame.size.width, containerView.frame.size.height);
fromViewController.view.layer.opacity = 0.f;
toViewController.view.layer.opacity = 1.f;
} else {
toViewController.view.frame = CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height);
toViewController.view.layer.opacity = 1.f;
fromViewController.view.layer.opacity = 0.3f;
}
} completion:^(BOOL finished) {
[transitionContext completeTransition:finished];
}];
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
if (self.reverse) {
return 0.45;
} else {
return 0.35;
}
}
How can I prevent my toViewController from disappearing if .reverse is set to YES?
Update: This is how I'm presenting the view controller:
SecondaryViewController *vc = [[SecondaryViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
navigationController.modalPresentationStyle = UIModalPresentationCustom;
navigationController.transitioningDelegate = self;
[self presentViewController:navigationController animated:YES completion:nil];
The container view disappearing on dismissal is correct behavior. Your mistake is adding the fromView to it.
You are incorrectly distinguishing whether this is presentation or dismissal and what you should do in each case. Simply use the two view controllers fromViewController and toViewController to tell them apart; on dismissal, the roles are reversed. On dismissal, do not add anything to the content view; the original presenter is still present and will be revealed by the removal of the container view.
So, on presentation, add only the toView to the container view. On dismissal, do not add anything to the container view. It's as simple as that.
My solution is setting modalPresentationStyle = .custom. It can be before or after the line transitioningDelegate = self.
TLDR:
This discussion regards to presenting transition only, because I didn't have much issue with dismissing transition.
My modal view is smaller than the screen and the presenting view is supposed to be shown in the background. But no, I got a totally black background instead of the presenting view as soon as transitionContext.completeTransition is called. I have a similar modal view with the presenting view in the background somewhere else in code, and it's totally working. After doing some comparison I eliminate all the other possibilities and narrow down to the difference of modalPresentationStyle = .custom, which has .fullScreen for the default value. I guess .fullScreen assumes the presented view takes full screen and decide to remove the presenter view (fromView) regardlessly.
One thing interesting with modalPresentationStyle = .custom: transitionContext.view(forKey: .from) returns nil while transitionContext.viewController(forKey: .from) still returns the presenting view controller.
How can I get custom transitions (iOS7) when pushing a view controller onto UINavigationController? I tried setting the TransitioningDelegate both in the UINavigationController and also on the controller I'm pushing
The methods never get called.
All examples I find use custom transitions when presenting modally.
#rounak has the right idea, but sometimes it helps to have code ready without having to download from github.
Here are the steps that I took:
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
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 animator class and paste these sample methods:
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
return 0.5f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
// Grab the from and to view controllers from the context
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// Set our ending frame. We'll modify this later if we have to
CGRect endFrame = CGRectMake(80, 280, 160, 100);
if (self.presenting) {
fromViewController.view.userInteractionEnabled = NO;
[transitionContext.containerView addSubview:fromViewController.view];
[transitionContext.containerView addSubview:toViewController.view];
CGRect startFrame = endFrame;
startFrame.origin.x += 320;
toViewController.view.frame = startFrame;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
toViewController.view.frame = endFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
else {
toViewController.view.userInteractionEnabled = YES;
[transitionContext.containerView addSubview:toViewController.view];
[transitionContext.containerView addSubview:fromViewController.view];
endFrame.origin.x += 320;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
fromViewController.view.frame = endFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
Essentially, the animator is the object doing the heavy lifting. Of course, you can make your UINavigationControllerDelegate be a separate object, but that depends on how your architect your app.
objc.io's post on view controller transitions are specifically for pushing and popping view controllers. http://objc.io/issue-5/view-controller-transitions.html
I've done this animation (http://i.imgur.com/1qEyMu3.gif) solely based on the objc.io post.
In short you have to have a class(es) implementing UINavigationControllerDelegate, and UIViewControllerAnimatedTransitioning with the required methods for returning the correct animator, and performing the animations.
You can look at my demo project which demonstrates using custom transitions in UINavigationController. Look at https://github.com/Vaberer/BlurTransition.
EDIT: Just realised this might not answer your question. But it is an alternative.
If you're using a storyboard you can do a custom transition by creating a custom segue.
In the attributes inspector change the segue class name to your custom transition class e.g. MySegue. Then create the MySegue class and implement the -(void)perform method to perform your transition.
- (void) perform{
UIViewController *source = self.sourceViewController;
UIViewController *destination = self.destinationViewController;
[UIView transitionFromView:source.view
toView:destination.view
duration:0.50f
options:UIViewAnimationOptionTransitionFlipFromTop
completion:nil];
}
I'm trying to build a custom transition in iOS 7. The transition occurs fine, but then when transition context complete transition the modal screen disappears from the view entirely. I've followed several tutorials and I don't see what I am doing wrong. In addition, if I don't call "complete transition" then the view stays, but will not receive any touch events. I checked in Reveal App and there is no view sitting on top of it. Any ideas?
Here is the method where I initiate the transition
- (IBAction)settingsButtonClicked:(id)sender
{
UINavigationController *navigationController =[[self storyboard] instantiateViewControllerWithIdentifier:#"SettingsNavigationViewController"];
navigationController.transitioningDelegate = self;
[self presentViewController:navigationController animated:YES completion:nil];
}
Here is the code for the custom transition:
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toViewController.view];
CGRect sourceRect = [transitionContext initialFrameForViewController:fromViewController];
CGRect initialTargetFrame = [transitionContext initialFrameForViewController:toViewController];
CGRect initialFrame = CGRectMake(sourceRect.size.width + initialTargetFrame.size.width, 0, initialTargetFrame.size.width, initialTargetFrame.size.height);
CGPoint destinationPoint = CGPointMake(sourceRect.size.width - 500, 0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(initialFrame.origin.x, initialFrame.origin.y);
toViewController.view.transform = translate;
[UIView animateWithDuration:PRESENT_DURATION delay:0 options:UIViewAnimationOptionCurveEaseOut
animations:^{
toViewController.view.transform = CGAffineTransformMakeTranslation(destinationPoint.x, destinationPoint.y);
}
completion:^(BOOL completed) {
if (completed) {
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}
}];
}
So I found out what I was doing wrong. I was changing the modalPresentationStyle to custom on the view controller that was being popped onto the navigation controller when I should have been setting it on the navigation controller itself. I added this line to the settingsButtonClicked method above and it worked properly.
navigationController.modalPresentationStyle = UIModalPresentationCustom;
I'm testing around the new iOS 7 custom transition API but i have some troubles with the navigation controller case. I tried a very basic test for the moment with this :
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
[transitionContext completeTransition:YES];
}
As you guessed, this code do nothing except to complete the transition with no animations.
But here's the problem : if it's working normally with present / dismiss a controller, all i see with push and pop methods is a black screen, as if [transitionContext completeTransition:YES] didn't work.
I've set all the delegate properties and delegate methods properly, since this method is called all the time (present, dismiss, push, pop).
Did someone already face this issue ?
Try something more like this, I was having trouble with it as well and this helped make more sense of it
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// 1. obtain state from the context
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
CGRect finalFrame = [transitionContext finalFrameForViewController:toViewController];
// 2. obtain the container view
UIView *containerView = [transitionContext containerView];
// 3. set initial state
CGRect screenBounds = [[UIScreen mainScreen] bounds]; toViewController.view.frame =
CGRectOffset(finalFrame, 0, screenBounds.size.height);
// 4. add the view
[containerView addSubview:toViewController.view];
// 5. animate
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:duration animations:^{
toViewController.view.frame = finalFrame;
} completion:^(BOOL finished) {
// 6. inform the context of completion
[transitionContext completeTransition:YES];
}];
}
Source: http://www.raywenderlich.com/forums/viewtopic.php?f=37&t=8851