ios 向 DetailViewContoller 添加滑动手势识别器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15165672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:31:28  来源:igfitidea点击:

Adding swipe gesture recognizer to DetailViewContoller

iosobjective-cipaduisplitviewcontroller

提问by darko_5

I have simple xcode project just made from "Master-Detail Application" template, for iPad. When device is in Portrait orientation, master view is hidden and when you swipe right on detail view, master view will show up. Now, i want to add right swipe gesture recognizer to detail view, like that:

我有一个简单的 xcode 项目,只是由“主从应用程序”模板制作的,适用于 iPad。当设备处于纵向时,主视图被隐藏,当您在详细视图上向右滑动时,将显示主视图。现在,我想向详细视图添加向右滑动手势识别器,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configureView];

    UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler)];
    [self.view addGestureRecognizer:gestureRecognizer];
}

-(void)swipeHandler{
    NSLog(@"SWIPE");
}

But this code causes that when i swipe on detail view, "SWIPE" log appears in console, but master view doesn't show up.

但是这段代码导致当我在详细视图上滑动时,控制台中会出现“滑动”日志,但主视图没有显示。

How to add right swipe gesture recognizer to detail view, so it wont prevent master view to show up and my handler for recognizer will work?

如何将向右滑动手势识别器添加到详细信息视图,这样它就不会阻止主视图显示并且我的识别器处理程序将起作用?

Thanks in advance.

提前致谢。

EDIT. I want my right swipe recognizer handler to work simultaneously with this built in one, which shows up master view, but that following code isn't a solution for this specific situation:

编辑。我希望我的右滑动识别器处理程序与这个内置的处理程序同时工作,它显示主视图,但以下代码不是针对这种特定情况的解决方案:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;
}

回答by nsgulliver

you should set the direction for the swipe in order to add the right swipe

您应该设置滑动方向以添加正确的滑动

UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
    [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:gestureRecognizer];

and your swipe handler might look like

你的滑动处理程序可能看起来像

-(void)swipeHandler:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
}

回答by Jagat Dave

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognizer:)];
    recognizer.direction = UISwipeGestureRecognizerDirectionRight;
    recognizer.delegate = self;
    [self.view addGestureRecognizer:recognizer];
}

- (void)swipeRecognizer:(UISwipeGestureRecognizer *)sender {
    if (sender.direction == UISwipeGestureRecognizerDirectionRight){
        [UIView animateWithDuration:0.3 animations:^{
            CGPoint Position = CGPointMake(self.view.frame.origin.x + 100.0, self.view.frame.origin.y);
            self.view.frame = CGRectMake(Position.x , Position.y , self.view.frame.size.width, self.view.frame.size.height);
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
}

回答by Nilesh Parmar

Try This

尝试这个

//Right Swipe

//向右滑动

UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizer];

//Left Swipe

//向左滑动

UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerLeft:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.view addGestureRecognizer:gestureRecognizer];

call method

调用方法

-(void)swipeHandlerRight:(id)sender
{
    //Your ViewController
}

-(void)swipeHandlerLeft:(id)sender
{
 //Your ViewController
}

回答by Mehsam Saeed

swift 4.0

迅捷 4.0

Step 1: Add swipe Gesture(s) in viewDidLoad() method.

步骤 1:在 viewDidLoad() 方法中添加滑动手势。

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeLeft.direction = .left
    self.view.addGestureRecognizer(swipeLeft)

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeRight.direction = .right
    self.view.addGestureRecognizer(swipeRight)


}

Step 2: Check the gesture detection in handleGesture() method

第 2 步:检查 handleGesture() 方法中的手势检测

@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
    if gesture.direction == UISwipeGestureRecognizerDirection.right {
        print("Swipe Right")
    }
    else if gesture.direction == UISwipeGestureRecognizerDirection.left {
        print("Swipe Left")
    }

}

回答by user1066524

This works. It pushes the view controller from the navigationController.

这有效。它从 navigationController 推送视图控制器。

- (void)viewDidLoad
{
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer           alloc] initWithTarget:self  action:@selector(swipeHandler:)];
     [self.view addGestureRecognizer:gestureRecognizer];
}

-(IBAction)swipeHandler:(UISwipeGestureRecognizer *)sender
{
      NSLog(@"SWIPE");
      UIViewController *tb = [[DetailViewController alloc] init];
      tb = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
      [self.navigationController pushViewController: tb animated:YES];
}

Then be sure to go to storyboard (or you could also do this manually) - click on the Detail View Controller and give the View controller the Identity: DetailViewController

然后确保转到故事板(或者您也可以手动执行此操作) - 单击 Detail View Controller 并为 View Controller 指定 Identity:DetailViewController