BhushanVU
BhushanVU

Reputation: 3455

UIViewController Animation ios?

Trying to create something like below in Objective-C, this is available in Swift. I tried using bridging etc, to use this code with my Objective C project but it's having import issues.

In Objective-C I tried below things and partially it works for UIView, but I guess I need to add transitions to complete UIViewController.

enter image description here

What I tried

- (IBAction)menuButtonPressed:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(menuButtonPressed)]) {
        [self.delegate menuButtonPressed];
    }
    if (!isVertical) {
        [UIView animateWithDuration: 0.6
                              delay: 0.0
             usingSpringWithDamping: 0.3
              initialSpringVelocity: .8
                            options: 0
                         animations: ^
         {
             self.transform = CGAffineTransformTranslate(self.transform, -240, 0);
             self.transform = CGAffineTransformRotate(self.transform, 90 * M_PI / 180);
             self.transform = CGAffineTransformTranslate(self.transform, 240, 0);
         }
                         completion:^(BOOL finished) {
                             isVertical = YES;

                         }
         ];

    }else{
        [UIView animateWithDuration: 0.45
                              delay: 0.0
             usingSpringWithDamping: 0.44
              initialSpringVelocity: .8
                            options: 0
                         animations: ^
         {
             self.transform = CGAffineTransformTranslate(self.transform, -240, 0);
             self.transform = CGAffineTransformRotate(self.transform, -90 * M_PI / 180);
             self.transform = CGAffineTransformTranslate(self.transform, 240, 0);

         }
                         completion:^(BOOL finished) {
                             isVertical = NO;

                         }
         ];
    }
}

Any inputs will be helpful.

Upvotes: 4

Views: 604

Answers (1)

Sandeep
Sandeep

Reputation: 21134

Here is a very simple implementation that you can use to animate transitions between view controllers. You could basically use the same animation gist if you want to use it with UIViews.

let generateRandomColor: Void -> UIColor = {
    let red = CGFloat(arc4random_uniform(255)) / 255.0
    let green = CGFloat(arc4random_uniform(255)) / 255.0
    let blue = CGFloat(arc4random_uniform(255)) / 255.0
    return UIColor(red:red, green: green, blue: blue, alpha: 1)
}

class SlideAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
    let presenting: Bool

    init(presenting: Bool) {
        self.presenting = presenting
        super.init()
    }

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()!

        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

        var animatingView: UIView
        var initialTransform: CGAffineTransform
        var finalTransform: CGAffineTransform

        if presenting {
            containerView.addSubview(toView)
            animatingView = toView
            initialTransform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
            finalTransform = CGAffineTransformIdentity
        } else {
            containerView.insertSubview(toView, belowSubview: fromView)
            animatingView = fromView
            initialTransform = CGAffineTransformIdentity
            finalTransform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
        }

        animatingView.layer.anchorPoint = CGPointMake(0, 0)
        animatingView.frame = containerView.bounds
        animatingView.transform = initialTransform

        UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 16, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
            animatingView.transform = finalTransform
            }) {
            _ in
                toView.layer.anchorPoint = CGPointMake(0.5, 0.5)
                toView.frame = containerView.bounds
                transitionContext.completeTransition(true)
        }
    }

}

class PresentedViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = generateRandomColor()

        let tapGestureRecognizer = UITapGestureRecognizer(target: self,
            action: "tapped")
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func tapped() {
        dismissViewControllerAnimated(true, completion: nil)
    }

}

class TestViewController: UIViewController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = generateRandomColor()
        let tapGestureRecognizer = UITapGestureRecognizer(target: self,
            action: "tapped")
        definesPresentationContext = true
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func tapped() {
        let presentedViewController = PresentedViewController()
        presentedViewController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
        presentedViewController.transitioningDelegate = self
        presentViewController(presentedViewController, animated: true, completion: nil)
    }

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return SlideAnimationController(presenting: true)
    }

    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return SlideAnimationController(presenting: false)
    }

}

Upvotes: 2

Related Questions