在OS X Yosemite中为ViewController定制动画

Imo*_*per 8 macos objective-c osx-yosemite

我想实现新方法,我在Google和StackOverflow上搜索了很多,但我还没有找到一个例子

- (void)presentViewController:(NSViewController *)viewController animator:(id <NSViewControllerPresentationAnimator>)animator
Run Code Online (Sandbox Code Playgroud)

这种方法在OSX 10.10中可用,并且该方法需要实现NSViewControllerPresentationAnimator具有这两种方法的协议

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

- (void)animateDismissalOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController 
Run Code Online (Sandbox Code Playgroud)

这个方法允许我们在两个NSViewController之间做自定义动画我需要一个实现的考试,我有这个代码

- (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
{


}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我一个如何实现这种转变的例子非常感谢.

Tho*_*cic 13

这是一个简单版本(Swift),在新的viewcontroller视图中淡出.我相信你可以把它翻译成Objective-C.

您将希望实际使用Autolayout而不仅仅是更改框架,但这会使示例更长一些(不太困难.只需在添加视图后添加约束)

我不确定你是否还需要viewcontroller遏制.然后需要对addChildViewController等进行适当的调用.也许有人可以说明何时可能需要或者在任何情况下它实际上都是好的做法.

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()
        })

    }
}
Run Code Online (Sandbox Code Playgroud)

希望这能让你开始!