2014-11-03 107 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