下面是呈现NSViewController的四种方式
- AsPopover
- (IBAction)presentAsPopover:(NSButton *)发送方{ NSViewController* vc = [MyViewController alloc :nil bundle:nil];自呈现视图控制器:vc asPopoverRelativeToRect:sender.frame ofView:self.view首选边缘:NSMinYEdge行为:NSPopoverBehaviorTransient瞬态;}
- AsSheet
- (IBAction)presentAsSheet:(NSButton *)发送方{ NSViewController* vc = [MyViewController alloc :nil bundle:nil];自呈现presentViewControllerAsSheet: vc;}
- WithAnimator
- (IBAction)presentWithAnimator:(NSButton *)发送方{ NSViewController* vc= [MyViewController alloc :nil bundle:nil];自呈现presentViewControllerAsModalWindow:vc;}
- WithAnimator
- (IBAction)presentWithAnimator:(NSButton*)发送方{ id animator =[MyViewController alloc ];NSViewController* vc = [MyViewController alloc :nil bundle:nil];self presentViewController:vc animator:animator;}
这是动画制作:
@interface MyCustomAnimator : NSObject <NSViewControllerPresentationAnimator>
@end
@implementation MyCustomAnimator
- to Show
- (void)animatePresentationOfViewController:(NSViewController *)viewController fromViewController:( NSViewController * )fromViewController { NSViewController* bottomVC = fromViewController;NSViewController* topVC = viewController;//确保视图有用于平滑动画的CA层topVC.view.wantsLayer = YES;//设置重绘策略topVC.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;//开始看不见的topVC.view.alphaValue = 0;//添加呈现的viewcontroller bottomVC.view addSubview:topVC.view的视图;//调整大小和颜色CGRect NSRectToCGRect= NSRectToCGRect(bottomVC.view.frame);frame = CGRectInset( frame,40,40);topVC.view setFrame:NSRectFromCGRect(frame);topVC.view.layer.backgroundColor = NSColor grayColor.CGColor;//做一些CoreAnimation东西来呈现视图NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { context.duration = 0.5;topVC.view.animator.alphaValue = 0.8;} completionHandler:nil;}
- to Hide
- (void)animateDismissalOfViewController:(NSViewController *)viewController fromViewController:( NSViewController * )fromViewController { //NSViewController* bottomVC = fromViewController;NSViewController* topVC = viewController;//确保视图有用于平滑动画的CA层topVC.view.wantsLayer = YES;//设置重绘策略topVC.view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;//执行一些上下文操作来呈现视图[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { context.duration = 0.5;topVC.view.animator.alphaValue = 0;} completionHandler:^{ topVC.view removeFromSuperview;}];} @end
请牢记
为了控制NSViewController的行为,
有了xib文件,就很容易控制NSViewController。
在没有xib文件或在storyboard中设置的情况下做这件事有点复杂