2014-11-03 14 views
8

Tôi muốn thực hiện phương pháp mới, tôi đã tìm kiếm rất nhiều trên Google và StackOverflow nhưng tôi đã không tìm thấy một ví dụAnimate tùy chỉnh trình bày của ViewController trong OS X Yosemite

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

phương pháp này là có sẵn trong OSX 10.10 và điều này phương pháp cần phải thực hiện các giao thức NSViewControllerPresentationAnimator Mà có này hai phương pháp

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

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

phương pháp này cho phép chúng ta làm phim hoạt hình tùy chỉnh giữa hai NSViewController của tôi cần một examople thực hiện, tôi có mã này

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


} 

Bất kỳ ai có thể giúp tôi làm ví dụ về cách tôi có thể triển khai quá trình chuyển đổi này Cảm ơn rất nhiều.

+0

Bạn có cần thêm trợ giúp hoặc ví dụ của tôi đủ không? –

+0

vâng, OK, câu trả lời của bạn đã giải quyết được vấn đề của tôi, cảm ơn rất nhiều ... – Imodeveloper

Trả lời

12

Đây là phiên bản đơn giản (Swift) mờ dần trong chế độ xem của chế độ xem mới. Tôi chắc chắn bạn có thể dịch thành Mục tiêu-C.

Bạn sẽ muốn thực sự sử dụng Autolayout thay vì chỉ thay đổi khung hình, nhưng điều đó sẽ làm ví dụ lâu hơn một chút (không quá khó khăn. Chỉ cần thêm những hạn chế sau khi bạn thêm xem)

Tôi không chắc chắn nếu bạn cần ngăn chặn viewcontroller là tốt. Sau đó, sẽ cần phải có các cuộc gọi thích hợp tới addChildViewController và v.v. Có lẽ ai đó có thể làm sáng tỏ khi điều này có thể là cần thiết hoặc nếu thực sự là thực hành tốt trong mọi trường hợp.

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

    } 
} 

Hy vọng điều này sẽ giúp bạn bắt đầu!

+1

Ồ, và hãy chắc chắn đọc http://jwilling.com/osx-animations trước khi thay đổi khung bằng cách sử dụng Core Animation. Đó là một bài viết tuyệt vời! –

Các vấn đề liên quan