我想实现新方法,我在 Google 和 Stack Overflow 上搜索了很多,但没有找到示例。
- (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
{
}
谁能帮我举例说明如何实现这一转变?
这是一个简单的版本(Swift),它在新视图控制器的视图中淡入淡出。
我相信你可以将其翻译成 Objective-C。
您将希望实际使用自动布局,而不仅仅是更改框架,但这会使示例更长一些(不是太难。只需在添加视图后添加约束即可)
我不确定您是否也需要视图控制器遏制。然后需要进行适当的调用添加ChildViewController等等。也许有人可以阐明何时需要这样做,或者在任何情况下这是否实际上是良好的做法。
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()
})
}
}
希望这能让您开始!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)