【问题标题】:Adding swipe gesture recognizer to DetailViewContoller将滑动手势识别器添加到 DetailViewContoller
【发布时间】:2013-03-01 19:48:23
【问题描述】:

我有一个简单的 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");
}

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

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

提前致谢。

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

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

    return YES;
}

【问题讨论】:

    标签: ios objective-c ipad uisplitviewcontroller


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我认为向右滑动实际上是默认方向,这不是问题所在
    • 是的,我做到了。我的识别器工作得很好,但是这个内置的不能再工作了,这就是问题所在。
    • 您是否正确设置了代理?对于gestureRecognizer?当您实现委托方法时
    • 是的,我已经检查过了,但我仍然只能让我的手势识别器工作,而 master vew 没有出现。
    • 试试这个属性可能会成功...self.splitViewController.presentsWithGesture = YES; //for showing the maset view
    【解决方案2】:
    - (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];
            }];
        }
    }
    

    【讨论】:

      【解决方案3】:

      试试这个

      //向右滑动

      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
      }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 我不确定如何处理您的回答。关于滑动方向的知识是否可以解决我的问题?
        • 我真的需要这两者:我的和内置的 regonizers 在同一个视图上工作,即细节视图。
        • 您想要自己进行滑动处理并禁用主视图的呈现,还是想要对正在呈现的主视图做出反应?
        • @MartinUllrich 我想做自己的滑动处理,但我不想停止主视图呈现。问题是,当我以某种方式添加自己的滑动处理时,它会停止呈现主视图
        • @darko_5 你试过我上面的想法了吗?在我找到在 xcode 上尝试的解决方案后,我编辑了我的原始帖子。它对你有用吗?您找到其他解决方案了吗?
        【解决方案5】:

        斯威夫特 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")
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-20
          • 1970-01-01
          • 1970-01-01
          • 2019-03-27
          相关资源
          最近更新 更多