【问题标题】:Recognition of swipe with two touches in UIScrollView在 UIScrollView 中识别两次触摸滑动
【发布时间】:2011-07-01 20:10:23
【问题描述】:

我有一个全屏尺寸的 UIScrollView(iPad,1024x768,横向模式)。所以我需要识别用两根手指向任何方向滑动。这就是我所拥有的(mainScroll 是我班级的财产):

//MyViewController.h
- (void)loadView {
     mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
     mainScroll.contentSize = CGSizeMake(1024*pageNumber, 768);
     mainScroll.pagingEnabled = YES;
     mainScroll.delegate = self;
     [self.view addSubview:mainScroll];

     GestureRecognizer *tapInterceptor = [[GestureRecognizer alloc] init];
     tapInterceptor.numberOfTouchesRequired = 2;
     tapInterceptor.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
     [mainScroll addGestureRecognizer:tapInterceptor];
     mainScroll.userInteractionEnabled = YES;
}

//GestureRecognizer.h
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     if ([touches count] > 1)
         NSLog(@"Started");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
     if ([touches count] > 1)
         NSLog(@"Moved");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     if ([touches count] > 1)
         NSLog(@"Ended");
}

我必须添加条件

if ([touches count] > 1)

因为它不仅适用于两次(或更多)触摸,也适用于一次。

但滑动(两次触摸)仍在滚动我的 UIScrollView。我该如何预防?那么如何在不影响滚动视图的情况下识别滑动呢?

【问题讨论】:

  • 那么您的编程问题是什么?要具体。
  • 问题是:如何识别滑动而不影响滚动视图?

标签: objective-c xcode ipad uiscrollview multi-touch


【解决方案1】:

UIGestureRecognizer 的文档很好地解释了以下三个属性:

cancelsTouchesInView — 如果手势识别器识别出它的手势,它会从他们的视图中解除该手势的剩余触摸(因此窗口不会传递它们)。窗口使用 (touchesCancelled:withEvent:) 消息取消先前传递的触摸。如果手势识别器无法识别其手势,则视图会接收多点触控序列中的所有触摸。

delaysTouchesBegan — 只要手势识别器在分析触摸事件时没有未能识别其手势,窗口就会在 UITouchPhaseBegan 阶段阻止向附加视图传递触摸对象。如果手势识别器随后识别出它的手势,则视图不会接收到这些触摸对象。如果手势识别器无法识别其手势,则窗口在调用视图的 touchesBegan:withEvent: 方法时传递这些对象(并可能在后续的 touchesMoved:withEvent: 调用中通知它当前的触摸位置)。

delaysTouchesEnded — 只要手势识别器在分析触摸事件时没有未能识别其手势,窗口就会在 UITouchPhaseEnded 阶段阻止向附加视图传递触摸对象。如果手势识别器随后识别出它的手势,则取消触摸(在 touchesCancelled:withEvent: 消息中)。如果手势识别器无法识别其手势,则窗口会在调用视图的 touchesEnded:withEvent: 方法时传递这些对象。

这里还有更多:UIGestureRecognizer

我认为您需要“delaysTouchesBegan”或“delaysTouchesEnded”,以便在手势失败之前,滚动视图不会收到任何触摸(IE 滚动视图)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多