禁用在iPad上使用手写笔滚动

时间:2017-04-22 11:29:10

标签: ios swift uiscrollview stylus-pen

我试图在UIScrollView上取消滚动,如果它来自手写笔(Apple Pencil)。

任何建议?

2 个答案:

答案 0 :(得分:1)

您可以在UIGestureRecognizer上设置allowedTouchTypes属性。

例如:

scrollView.panGestureRecognizer.allowedTouchTypes = [UITouchType.direct.rawValue as NSNumber]

答案 1 :(得分:0)

你可以通过UITouch的类型找出触控是否来自手写笔。

if (touch.type == UITouchTypeStylus) {
     //its touch from Stylus.
}

现在,对于scrollview,您可以创建UIScrollview的子类并实现TouchBegan方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    if (touch.type == UITouchTypeStylus) {
       self.scrollEnabled = NO;
     }
    else
    {
       self.scrollEnabled = YES;
    }
    [super touchesBegan:touches withEvent:event];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if (touch.type == UITouchTypeStylus) {
      self.scrollEnabled = NO;
    }
    else
   {
      self.scrollEnabled = YES;
   }
   [super touchesMoved:touches withEvent:event];

}

//编辑 OR

子类UIApplication:

 @interface MyUIApplication : UIApplication
  - (void)sendEvent:(UIEvent *)event {

        [super sendEvent:event];
        NSSet *allTouches = [event allTouches];
        if ([allTouches count] > 0) {

             / 
             UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
             if (phase == UITouchPhaseBegan){

                   UITouch *touch = [allTouches  anyObject];


                if (touch.type == UITouchTypeStylus) {


                   [[NSNotificationCenter defaultCenter] postNotificationName:@"DisableScroll"  object:self];
                 }
               else
                {

                   [[NSNotificationCenter defaultCenter] postNotificationName:@"EnableScroll"  object:self];
                 }
             }

        }
    }
    @end




int main(int argc, char *argv[])
    {
          @autoreleasepool {
                return UIApplicationMain(argc, argv, NSStringFromClass([MyUIApplication class]), NSStringFromClass([AppDelegate class]));
          }
    }

在包含滚动视图的类中添加这些通知的观察者,并相应地启用/禁用滚动。

相关问题