我已经制作了一个应用程序,您可以在其中摇动手机以打开新视图。所有三个视图,当你在最后一个视图上摇动手机时,你会回到第一个屏幕。当我用自己的.xib创建新的子类控件视图时,这很好用。但我想在故事板项目中使用它,我需要改变什么?
事先非常感谢!
这是代码.H:
#import <UIKit/UIKit.h>
#import "FirstScreenViewController.h"
#import "SecondScreenViewController.h"
@interface ViewController:UIViewController
{
NSInteger currentScreen;
UIViewController* currentController;
}
@end
和你在.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;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(motion == UIEventSubtypeMotionShake)
{
if(currentController)
{
[currentController.view removeFromSuperview]; currentController =无;
}
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
答案 0 :(得分:1)
您需要将所有三个视图控制器添加到故事板,并在它们之间具有分段(包括从第三个到第一个的第一个)和附加到每个场景的摇动手势识别器。
每个手势识别器的动作方法告诉视图控制器performSegue:
具有适当的segue标识符。