开发者

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

I am coming from an iOS background and am new to Mac OSX (coco app) development.

Starting with apple sample code project "Simple Cocoa App" as a base, I want to be able to switch between different "View Controllers" or even just between NSViews in any manner similar to that of iOS apps.

Unfortunately, I could not find a way to accomplish this -- and the internet is rather lacking in resources tied to keywords like "View switching开发者_如何学Go in cocoa apps, switching views in mac apps, mac app dev tutorials"...

Do any of you know a tutorial that, and here's the kicker, actually covers the matter of switching between views? Or perhaps know of any quick way you might be able to explain in your Stack Overflow answer?

Proof Of Concept

A very simple proof-of-concept could be the following. In a simple app, there are two views - Screen 1 (currently displayed) and Screen 2 (hidden off screen):

STEP 1) Start app, Screen 1 appears (contains Label "Screen 1" and Button "Go Next")

STEP 2) Press the Button

STEP 3) Screen 1 slides offscreen as

STEP 4) Screen 2 slides in (contains a single Label "Screen 2")

Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜