容易切换“视图控制器”在Mac应用程序(类似于iOS) [英] Easy Switching of "View Controllers" in Mac Apps (similar to iOS)

查看:358
本文介绍了容易切换“视图控制器”在Mac应用程序(类似于iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自iOS背景,是Mac OSX(coco app)开发的新手。

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

从苹果示例代码项目简单Cocoa应用程序作为基础,我想能够在不同的视图控制器之间切换,甚至只是在NSViews之间以任何方式类似于iOS应用程序。

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.

不幸的是,我找不到一种方法来实现这一点 - 互联网相当缺乏与关键字相关的资源,如查看可换应用切换,切换视图Mac应用程序,mac应用开发教程...

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

任何人都知道一个教程,这里是踢球者,实际上 em> 视图之间切换的问题?或者也许知道你可以在你的Stack Overflow答案中解释什么快速的方法?

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?

概念证明

一个非常简单的概念验证可以是下面的。在一个简单的应用程序中,有两个视图 - 屏幕1(当前显示)和屏幕2(隐藏屏幕关闭):

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):

步骤1)启动应用程序,包含标签屏幕1和按钮下一步)

步骤2)按下按钮

步骤3)屏幕1从屏幕滑动为

步骤4)屏幕2张幻灯片(包含单个标签屏幕2)

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")

谢谢。

推荐答案

核心动画不像在iOS上那样深入到OS X中。
因此,你需要自己做很多。
你可以做的是使用窗口的contentView作为superview,然后执行以下操作切换到另一个视图(动画)。

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];
}

这不会给你想要的效果。如果你想要给它一个排序效果,你必须包括QuartzCore.framework并执行以下操作:

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];
}

那么在switchSubViews中:你只需要使用:

then in the switchSubViews: you only have to use:

[[mainView animator] addSubView:newSubView];

这篇关于容易切换“视图控制器”在Mac应用程序(类似于iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆