Animate custom presentation of ViewController in OS X Yosemite

前端 未结 1 1107
猫巷女王i
猫巷女王i 2020-12-29 15:00

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

- (void)presentViewController:(NSViewController         


        
1条回答
  •  礼貌的吻别
    2020-12-29 15:34

    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!

    0 讨论(0)
提交回复
热议问题