6

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 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.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
m0rtimer
  • 2,023
  • 1
  • 25
  • 31

2 Answers2

17

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];
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52
  • When i use the above code, at the line "[mainView removeAllSubViews];", it shows the following error... "No visible @interface for 'NSView' declares the selector 'removeAllSubViews'". What is the reason – nbs Apr 20 '13 at 09:46
  • @NidhinBalakrishnan that method is quite a new API, as I stated in the comment on that line you will have to manually remove all subviews ( loop through the array of subviews and remove one by one ) to get the desired effect. – Antwan van Houdt Apr 24 '13 at 06:48
3

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

sidyll
  • 57,726
  • 14
  • 108
  • 151