2

I was looking around for a solution on how to wrap a view in a dialog in iOS when I came across this post, which has this line:

vc.modalPresentationStyle = UIModalPresentationCurrentContext;

It solves my problem of basically creating/imitating a dialog but it does not animate upon transition as mentioned in the post. So what is the simplest way to get the slide up animation?

ps.I would ask this as a sub question in that post but I do not have 50 rep comment :(

Community
  • 1
  • 1
Naveed
  • 2,942
  • 2
  • 25
  • 58

1 Answers1

4

Well, once your view has been shown, you can do pretty much any animation you want in it. You can do a simple [UIView animateWithDuration] kind of deal, but I would personally use a CATransition for this, it's relatively simple.

The Way of the QuartzCore

First, I'm gonna assume that the view you're presenting is transparent, and there's another view inside that behaves as the dialog. The view controller that will be presented, let's call it PresentedViewController and holds the dialog property for the view within.

PresentedViewController.m

(Needs to be link against QuartzCore.h)

#import <QuartzCore/QuartzCore.h>

@implementation PresentedViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (animated)
    {
        CATransition *slide = [CATransition animation];

        slide.type = kCATransitionPush;
        slide.subtype = kCATransitionFromTop;
        slide.duration = 0.4;
        slide.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

        slide.removedOnCompletion = YES;

        [self.dialog.layer addAnimation:slide forKey:@"slidein"];
    }
}

Getting Fancy

The good thing about this, is that you can create your own custom animations, and play around with other properties.

CABasicAnimation *animations = [CABasicAnimation animationWithKeyPath:@"transform"];

CATransform3D transform;

// Take outside the screen
transform = CATransform3DMakeTranslation(0, self.view.bounds.size.height, 0);

// Rotate it
transform = CATransform3DRotate(transform, M_PI_4, 0, 0, 1);

animations.fromValue    = [NSValue valueWithCATransform3D:transform];
animations.toValue      = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animations.duration     = 0.4;
animations.fillMode     = kCAFillModeForwards;
animations.removedOnCompletion = YES;
animations.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0 :0.0 :0 :1];

[self.dialog.layer addAnimation:animations forKey:@"slidein"];

Here, the view will be moved outside of screen by the translation, then rotated, and it will slide in, back to its original transform. I also modified the timing function to provide a smoother curve.

Consider that I just scraped the surface of what's possible with CoreAnimation, I've been on this road for three years now, and I've grown to like CAAnimation for all the things it does.

Storyboard pro-tip: You can wrap this up very nicely if you build your own custom UIStoryboardSegue subclass.

Can
  • 8,502
  • 48
  • 57
  • Works perfect. Do you know why the animation is disabled in the first place? – Naveed Jul 30 '13 at 03:23
  • Not entirely sure, I've ran into this issue before, but I assumed it was normal. I'm still looking around and trying on simulator, I'll update if I find anything. (I also found [this other post](http://stackoverflow.com/questions/12736394/uimodalpresentationcurrentcontext-with-transition), but it didn't actually answer it) – Can Jul 30 '13 at 03:29
  • Nope, couldn't find anything, even the [View presentation documentation](http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW1) is short on details. It has some useful things (like, the frame is set by whoever defines the context) but aside from that, no word on transitions. – Can Jul 30 '13 at 04:11
  • 1
    @Naveed Added some extra code for a cooler animation, if you still care about that. – Can Jul 30 '13 at 04:59
  • Thanks definitely looking into creating some custom segue animations. – Naveed Jul 30 '13 at 14:29