在OS X Yosemite中对ViewController的动画自定义表示进行动画处理 [英] Animate custom presentation of ViewController in OS X Yosemite

查看:156
本文介绍了在OS X Yosemite中对ViewController的动画自定义表示进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现新方法,我在Google和StackOverflow上进行了很多搜索,但没有找到示例

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

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

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 

此方法允许我们在两个NSViewController的之间进行自定义动画 我需要一个实施示例,我有以下代码

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.

推荐答案

这是一个简单的版本(Swift),在新的viewcontroller的视图中淡出. 我相信您可以将其转换为Objective-C.

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)

我不确定您是否也需要Viewcontroller包含.然后需要对 addChildViewController 进行适当的调用,依此类推.也许有人可以在何时有必要或在任何情况下都将其视为一种很好的做法时提供一些启示.

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!

这篇关于在OS X Yosemite中对ViewController的动画自定义表示进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆