在OS X Yosemite中为ViewController定制动画

时间:2014-11-03 13:44:47

标签: objective-c macos osx-yosemite

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

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

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

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

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

这个方法允许我们在两个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
{


}

任何人都可以帮我提供一个如何实现此转换的示例 非常感谢。

1 个答案:

答案 0 :(得分: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()
        })

    }
}

希望这能让你开始!