Easy Switching of “View Controllers” in Mac Apps (similar to iOS)

只愿长相守 提交于 2019-11-30 02:29:34
Antwan van Houdt

Core Animation is not as deeply integrated in OS X as it is on iOS. Therefore you will need to do a lot yourself. What you could do is use the window's contentView as a superview and then do the following to switch to another view ( animated ).

- (void)switchSubViews:(NSView *)newSubview
{
  NSView *mainView = [[self window] contentView];
  // use a for loop if you want it to run on older OS's
  [mainView removeAllSubViews];

  // fade out
  [[mainView animator] setAplhaValue:0.0f];

  // make the sub view the same size as our super view
  [newSubView setFrame:[mainView bounds]];
  // *push* our new sub view
  [mainView addSubView:newSubView];

  // fade in
  [[mainView animator] setAlphaValue:1.0f];
}

This won't give you your desired effect however. If you want it to give it a sorta move effect you have to include QuartzCore.framework and do the following:

- (void)prepareViews
{ // this method will make sure we can animate in the switchSubViewsMethod
  CATransition *transition = [CATransition animation];
  [transition setType:kCATransitionPush];
  [transition setSubtype:kCATransitionFromLeft];
  NSView *mainView = [[self window] contentView];
  [mainView setAnimations:[NSDictionary dictionaryWithObject:transition forKey:@"subviews"]];
  [mainView setWantsLayer:YES];
}

then in the switchSubViews: you only have to use:

[[mainView animator] addSubView:newSubView];

You may want to check PXNavigationBar by Perspx, it can handle your situation in a much more iOS like.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!