22

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

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

5 回答 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.");
}
于 2013-03-01T19:53:04.807 回答
19
- (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];
        }];
    }
}
于 2015-01-09T12:36:09.563 回答
10

尝试这个

//向右滑动

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
}
于 2015-11-24T07:17:57.590 回答
0

这行得通。它从 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];
}

然后确保转到情节提要(或者您也可以手动执行此操作) - 单击详细视图控制器并为视图控制器提供标识:DetailViewController

于 2013-03-01T20:05:57.527 回答
0

迅捷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")
    }

}
于 2018-05-02T09:34:18.720 回答