【问题标题】:Disable scrolling with stylus on iPad在 iPad 上禁用手写笔滚动
【发布时间】:2017-09-19 09:34:21
【问题描述】:

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

有什么建议吗?

【问题讨论】:

    标签: ios swift uiscrollview stylus-pen


    【解决方案1】:

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

    例如:

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

    【讨论】:

      【解决方案2】:

      你可以通过UITouch的类型来判断触摸是否来自手指的手写笔。

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

      现在,对于滚动视图,您可以创建 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];
      
      }
      

      // 编辑 或

      子类 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]));
                }
          }
      

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

      【讨论】:

      • 我已经尝试过了,但由于某种原因,在滚动开始后会触发 touchesBegan,所以下次我触摸时会应用 scrollEnabled。 (我第一次用手写笔滚动时滚动有效)
      • 这个函数scrollViewWillBeginDragging(存在于UIScrollView委托中)在其他任何事情之前开始触发(甚至在touchesBegan之前)。如果此时有任何方法可以获取触摸类型,我认为它应该可以工作。
      • 如果是这种情况,您可以继承 UIApplication 并使用 sendEvent 方法。让我在答案中添加更多细节。
      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2014-05-11
      • 1970-01-01
      • 2011-12-05
      • 2011-07-22
      相关资源
      最近更新 更多