Animate custom presentation of ViewController in OS X Yosemite

大城市里の小女人 提交于 2019-11-29 03:18:15

问题


I want to implement new method, I searched a lot on Google and StackOverflow but I have not found an example

- (void)presentViewController:(NSViewController *)viewController animator:(id <NSViewControllerPresentationAnimator>)animator

this method is available in OSX 10.10 and this method need to implement the protocol NSViewControllerPresentationAnimator wich has this two methods

- (void)animatePresentationOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController 

- (void)animateDismissalOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController 

this methods allow us to do custom animation between two NSViewController's I need an examople of implementation, i have this code

- (IBAction)openTask:(id)sender {

    NSStoryboard *storyboard = [NSStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    Tasks *task = [storyboard instantiateControllerWithIdentifier:@"tasks"];
    [self presentViewController:task animator:self];

}

- (void)animatePresentationOfViewController:(NSViewController *)viewController
                         fromViewController:(NSViewController *)fromViewController
{


}

- (void)animateDismissalOfViewController:(NSViewController *)viewController
                      fromViewController:(NSViewController *)fromViewController
{


}

Can anyone help me with an example of how I could have implemented this transition Thanks a lot.


回答1:


Here is a simple version (Swift) that fades in the new viewcontroller's view. I am sure you can translate that into Objective-C.

You will want to actually use Autolayout instead of just changing the frame, but that would have made the example a bit longer (not too difficult. Just add constraints after you add the view)

I am not sure if you need viewcontroller containment as well. Then there would need to be the appropriate calls to addChildViewController and so on. Maybe someone can shed some light on when this might be necessary or if it is actually good practice in any case.

class MyTransitionAnimator: NSObject, NSViewControllerPresentationAnimator {

    func animatePresentationOfViewController(viewController: NSViewController, fromViewController: NSViewController) {

        let bottomVC = fromViewController
        let topVC = viewController

        // make sure the view has a CA layer for smooth animation
        topVC.view.wantsLayer = true

        // set redraw policy
        topVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay

        // start out invisible
        topVC.view.alphaValue = 0

        // add view of presented viewcontroller
        bottomVC.view.addSubview(topVC.view)

        // adjust size
        topVC.view.frame = bottomVC.view.frame

        // Do some CoreAnimation stuff to present view
        NSAnimationContext.runAnimationGroup({ (context) -> Void in

            // fade duration
            context.duration = 2
            // animate to alpha 1
            topVC.view.animator().alphaValue = 1

        }, completionHandler: nil)

    }

    func animateDismissalOfViewController(viewController: NSViewController, fromViewController: NSViewController) {

        let bottomVC = fromViewController
        let topVC = viewController

        // make sure the view has a CA layer for smooth animation
        topVC.view.wantsLayer = true

        // set redraw policy
        topVC.view.layerContentsRedrawPolicy = .OnSetNeedsDisplay

        // Do some CoreAnimation stuff to present view
        NSAnimationContext.runAnimationGroup({ (context) -> Void in

            // fade duration
            context.duration = 2
            // animate view to alpha 0
            topVC.view.animator().alphaValue = 0

        }, completionHandler: {

            // remove view
            topVC.view.removeFromSuperview()
        })

    }
}

Hope this gets you started!



来源:https://stackoverflow.com/questions/26715636/animate-custom-presentation-of-viewcontroller-in-os-x-yosemite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!