我已经做了一个应用程序,您摇动手机打开一个新的视图。所有这三个视图一起,当您在最后一个视图中摇动手机时,您将返回到第一个屏幕。当我用自己的.xib创建新的子类控件视图时,这工作正常。但我想在故事板项目中使用它,我需要改变什么?如何在使用故事板时使用摇动手势?
非常感谢!
以下是在.H的代码:
#import <UIKit/UIKit.h>
#import "FirstScreenViewController.h"
#import "SecondScreenViewController.h"
@interface的ViewController:UIViewController中
{
NSInteger currentScreen;
UIViewController* currentController;
}
@end
这里IN THE .M:
#import "ViewController.h"
@implementation的ViewController
- (无效)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark shake
- (BOOL)canBecomeFirstResponder
{
return true;
}
- (无效)motionEnded:(UIEventSubtype)运动withEvent:方法(的UIEvent *)事件
{
if(motion == UIEventSubtypeMotionShake)
{
如果(currentController)
{
[currentController。查看removeFromSuperview]; currentController = nil;
}
switch (currentScreen)
{
case 0:
currentController = [[FirstScreenViewController alloc] initWithNibName:@"FirstScreenViewController" bundle:nil];
break;
case 1:
currentController = [[SecondScreenViewController alloc] initWithNibName:@"SecondScreenViewController" bundle:nil];
}
if(currentController)
{
[currentController.view setFrame:self.view.bounds];
[self.view addSubview:currentController.view];
}
currentScreen++;
if(currentScreen >2)
currentScreen=0;
}
}
#pragma mark - View lifecycle
- (无效)viewDidLoad中
{
[super viewDidLoad]; currentScreen = 0;
}
- (无效)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
如果你的代码格式正确,你更可能得到帮助。 – 2012-02-15 20:46:10
现在我编辑了。这个可以吗? – 2012-02-16 07:08:32
通过粘贴它来格式化代码,选择然后单击问题编辑器上方的{}按钮。或者,缩进4个空格。 – jrturton 2012-02-16 07:19:39