使用故事板时如何使用摇动手势?
我制作了一个应用程序,您可以摇动手机以打开新视图。总共三个视图,当您在最后一个视图上摇动手机时,您会返回到第一个屏幕。当我使用自己的 .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
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark shake
-(BOOL)canBecomeFirstResponder
{
return true;
}
-(void)motionEnded:(UIEventSubtype)运动 withEvent:(UIEvent *)event
{
if(motion == UIEventSubtypeMotionShake)
{
if (currentController)
{
[currentController.view removeFromSuperview]; 当前控制器=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
-(void)viewDidLoad
{
[super viewDidLoad]; 当前屏幕 = 0;
}
-(void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
I've made an application where you shake the phone to open a new view. All together three views, and when you shake the phone on the last view you go back to the first screen. This works fine when I'm creating new subclass controls view with their own .xib. But I would like to use this in a storyboard project, what do I need to change?
Thanks a lot on beforehand!
HERE IS THE CODE IN .H:
#import <UIKit/UIKit.h>
#import "FirstScreenViewController.h"
#import "SecondScreenViewController.h"
@interface ViewController : UIViewController
{
NSInteger currentScreen;
UIViewController* currentController;
}
@end
AND HERE IN THE .M:
#import "ViewController.h"
@implementation ViewController
-(void)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=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
-(void)viewDidLoad
{
[super viewDidLoad];
currentScreen = 0;
}
-(void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将所有三个视图控制器添加到故事板,并在它们之间进行转场(包括从第三个返回到第一个),并将摇动手势识别器附加到每个场景。
每个手势识别器的操作方法告诉视图控制器使用适当的segue标识符来
performSegue:
。You need to add all three view controllers to the storyboard, and have segues between them (including one back to the first from the third) and a shake gesture recogniser attached to each scene.
The action method for each gesture recogniser tells the view controller to
performSegue:
with the appropriate segue identifier.