Ios 使用滑动手势切换视图控制器
好吧,这就是我遇到的问题: 我正试图从一个名为Ios 使用滑动手势切换视图控制器,ios,objective-c,viewcontroller,uiswipegesturerecognizer,Ios,Objective C,Viewcontroller,Uiswipegesturerecognizer,好吧,这就是我遇到的问题: 我正试图从一个名为MenuViewController的viewController中切换,其中包含我的菜单(显然)。我有一个单独的viewController名为viewController,其中包含我的mapView。我希望能够用双手指从我的菜单中向左滑动,然后切换到我的地图视图中 我不确定从哪里开始 此外,我使用的是xib文件,而不是故事板。运行iOS 6 首先,您不能使用内置的导航机制 您需要将“ViewController”的视图添加到“MenuViewCo
MenuViewController
的viewController
中切换,其中包含我的菜单(显然)。我有一个单独的viewController
名为viewController
,其中包含我的mapView
。我希望能够用双手指从我的菜单中向左滑动,然后切换到我的地图视图中
我不确定从哪里开始
此外,我使用的是xib文件,而不是故事板。运行iOS 6 首先,您不能使用内置的导航机制
您需要将“ViewController”的视图添加到“MenuViewController”的视图中,我建议您将“ViewController”作为子视图控制器添加到“MenuViewController”中
之后,将“ViewController”的框架设置在屏幕外侧,当您滑动或做手势时,只需将其动画化回到屏幕上即可。这听起来是一个使用的最佳时机,或者更具体地说,一旦完成此操作
UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.view addGestureRecognizer:swipeLeftGesture];
swipeLeftGesture.direction=UISwipeGestureRecognizerDirectionLeft;
-(void)handleSwipeGesture:(UIGestureRecognizer *) sender
{
NSUInteger touches = sender.numberOfTouches;
if (touches == 2)
{
if (sender.state == UIGestureRecognizerStateEnded)
{
//Add view controller here
}
}
}
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleleftSwipe:)];
swipeLeft.numberOfTouchesRequired = 1;//give required num of touches here ..
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delegate = (id)self;
[self. view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handlerightSwipe:)];
swipeRight.numberOfTouchesRequired = 1;//give required num of touches here ..
swipeRight.delegate = (id)self;
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
现在定义如下所示的滑动方法:
-(void)handleleftSwipe:(UISwipeGestureRecognizer *)recognizer{
//Do ur code for Push/pop..
}
-(void)handlerightSwipe:(UISwipeGestureRecognizer *)recognizer{
//Do ur code for Push/pop..
}
希望它能帮助你……试试这个
在.h文件中
@interface MenuViewController : UIViewController {
ViewController *mapViewObj;
}
-(void) viewDidLoad {
[super viewDidLoad];
mapViewObj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.view addGestureRecognizer:swipeLeftGesture];
swipeLeftGesture.direction=UISwipeGestureRecognizerDirectionLeft;
}
-(void)handleSwipeGesture:(UIGestureRecognizer *) sender {
NSUInteger touches = sender.numberOfTouches;
if (touches == 2) {
if (sender.state == UIGestureRecognizerStateEnded) {
//push mapViewObj over here..
[self.navigationController pushViewController:mapViewObj animated:YES];
}
}
}
在.m文件中
@interface MenuViewController : UIViewController {
ViewController *mapViewObj;
}
-(void) viewDidLoad {
[super viewDidLoad];
mapViewObj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.view addGestureRecognizer:swipeLeftGesture];
swipeLeftGesture.direction=UISwipeGestureRecognizerDirectionLeft;
}
-(void)handleSwipeGesture:(UIGestureRecognizer *) sender {
NSUInteger touches = sender.numberOfTouches;
if (touches == 2) {
if (sender.state == UIGestureRecognizerStateEnded) {
//push mapViewObj over here..
[self.navigationController pushViewController:mapViewObj animated:YES];
}
}
}
这是我给你编的代码
//add gesture recogniser to your view
[self addSwipegestureToView:self.view];
- (void) addSwipegestureToView : (UIView *) view{
UISwipeGestureRecognizer *_swipegestureRecogniser = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesturePerformed)];
_swipegestureRecogniser.numberOfTouchesRequired = 2;
[_swipegestureRecogniser setDirection:UISwipeGestureRecognizerDirectionLeft];
[view addGestureRecognizer:_swipegestureRecogniser];
}
- (void) swipeGesturePerformed{
SecondViewController *object = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:object animated:YES];
}
您只需要拥有的是,currentViewController
必须拥有navigationController
,才能进行适当的推入(滑入)导航。啊,没用,好先生。我把它加载到我的头文件和实现文件中。我也不确定我是否做得正确,但我实际上使用的是谷歌地图API。mapView是否不直接进入默认的ViewController?不过,我很感谢您的努力。请在第[self.navigationController-pushViewController:mapViewObj-animated:YES]行替换代码;对不起,我没听懂。我应该用什么替换这段代码?这将进入我的viewDidLoad正确吗?我的头文件中有什么?另外,我该如何“在此添加视图控制器”?将滑动手势代码(ans中的前3行)放入viewDidLoad中。.h文件中没有任何内容。分配您的视图控制器&只需在HandleSweepSpiration方法中推送、添加或显示它。这就是我将我的视图控制器添加到该行的方式,但它不起作用:viewController*mapView=[[viewController alloc]initWithNibName:@“viewController”bundle:[NSBundle mainBundle]];[self.navigationController pushViewController:mapView动画:是];您在哪里分配导航控制器?只需解释如何在其上添加视图控制器。