将滑动手势识别器添加到详细视图控制器

22

我有一个简单的Xcode项目,只是使用了“Master-Detail Application”模板,适用于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");
}

但是这段代码导致当我在详细视图上滑动时,“SWIPE”日志会出现在控制台中,但主视图不会显示出来。
如何为详细视图添加右滑手势识别器,以便它不会阻止主视图的显示,并且我的识别器处理程序将正常工作?
提前感谢。
编辑。我希望我的右滑识别器处理程序能够与内置的识别器同时工作,该识别器可以显示主视图,但是以下代码对于这种特定情况并不是解决方案:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;
}
5个回答

38
你需要设置滑动方向才能添加正确的滑动。
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandler:)];
    [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:gestureRecognizer];

而您的滑动处理程序可能如下所示

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

我认为向右滑动实际上是默认方向,这不是问题所在。 - darko_5
是的,我做到了。我的识别器完美地工作,但这个内置的不再工作了,这就是问题所在。 - darko_5
1
你是否正确设置了gestureRecognizer的委托?因为你正在实现委托方法。 - nsgulliver
请查看这个链接,它可能对你的特定问题有所帮助:http://stackoverflow.com/questions/9628107/master-table-application - nsgulliver
[self.view addGestureRecognizer:recognizer]; // 不是 gestureRecognizer - tdios
显示剩余3条评论

18
- (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];
        }];
    }
}

9
尝试这个
//向右滑动
UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizer];

//向左滑动

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

调用方法

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

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

0

Swift 4.0

步骤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)


}

步骤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")
    }

}

0

这个可以用。它从导航控制器中推送视图控制器。

- (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];
}

然后确保进入Storyboard(或者您也可以手动完成此操作)- 点击Detail View Controller并将View controller的Identity设置为:DetailViewController


我不确定如何处理你的答案。了解滑动方向能否为我解决问题? - darko_5
我真的需要我的和内置识别器在同一个视图上工作,这个视图是详细视图。 - darko_5
你想自己处理滑动操作并禁用主视图的显示,还是想在主视图被呈现时做出反应? - Martin Ullrich
@MartinUllrich 我想自己处理滑动操作,但我不想停止主视图的呈现。问题是,当我添加自己的滑动操作时,它会以某种方式停止呈现主视图。 - darko_5
@darko_5 你试过我上面的想法了吗?在Xcode上尝试后,我编辑了我的原始帖子并找到了解决方案。它对你有用吗?你找到了另一个解决方案吗? - user1066524
@user1066524 我已经尝试过这段代码,它非常有趣。但是主视图需要显示而不是细节。当您在纵向模式下滑动详细视图时,主视图将默认显示,即使我添加自己的滑动处理程序,我仍然需要它显示。 - darko_5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接