gpt4 book ai didi

objective-c - 在 OS X Yosemite 中对 ViewController 进行动画自定义演示

转载 作者:太空狗 更新时间:2023-10-30 03:31:37 25 4
gpt4 key购买 nike

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

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

此方法在 OSX 10.10 中可用,此方法需要实现具有这两个方法的协议(protocol) 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
{


}

任何人都可以帮我举个例子说明我如何实现这种转变吗?

最佳答案

这是一个在新 View Controller 的 View 中淡出的简单版本 (Swift)。我相信您可以将其转化为 Objective-C。

你会想要实际使用自动布局而不是仅仅改变框架,但这会使示例更长一点(不太难。只需在添加 View 后添加约束)

我不确定您是否也需要 viewcontroller containment。然后需要适当调用 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()
})

}
}

希望这能让您入门!

关于objective-c - 在 OS X Yosemite 中对 ViewController 进行动画自定义演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26715636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com