2014-11-03 87 views
8

我要实现新的方法,我搜索谷歌和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的 我需要实现的examople,我有这样的代码

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


} 

任何人都可以帮助我举一个例子,说明如何实现这个转换 非常感谢。

+0

有没有更多的帮助,你需要或我的例子足够? – 2014-11-04 12:06:48

+0

是的,没关系,你的答案解决了我的问题,非常感谢... – Imodeveloper 2014-11-04 14:54:31

回答

12

下面是一个简单的版本(SWIFT),在新的ViewController的看法变淡。 我相信你可以把它翻译成Objective-C。

您确实希望使用自动布局,而不是仅仅改变了框架,但会作出的例子多一点的时间(不太难。只需添加约束添加视图后)

我不知道如果你还需要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() 
     }) 

    } 
} 

希望这能让你开始!

+1

哦,并且确保在使用Core Animation更改帧之前阅读http://jwilling.com/osx-animations。这是一篇很棒的文章! – 2014-11-03 16:07:19