如何告诉我的应用忽略特定的触摸?

时间:2019-01-25 02:42:05

标签: ios swift uitouch

我的iPad应用必须能够完全消除特定的触摸,即手指或手写笔的触摸,而不是来自手掌的触摸。该视图启用了多点触摸,因此我可以分别分析每个触摸。

我目前可以使用majorRadius属性来区分这些触摸,但是我不确定如何能够消除较大的触摸,即大于我确定的阈值的触摸。

let touchThreshold : CGFloat = 21.0

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        if touch.majorRadius > touchThreshold {
            //dismiss the touch(???)
        } else {
            //draw touch on screen
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        if touch.majorRadius > touchThreshold {
            //dismiss the touch(???)
        } else {
            //draw touch on screen
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以改用此方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch;

您可以在其中检查手势的类型,例如,gesturerecognizer的类型是否为UITapGestureRecognizer

您可以检查触摸长半径是否在阈值限制内,并且是否在阈值内,则通过true,否则通过false。

   - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
                                       shouldReceiveTouch:(UITouch *)touch
         {
                 if touch.majorRadius > touchThreshold 
                  {
                      //dismiss the touch(???)
                     return No;
                  } 
                  else 
                  {
                       //draw touch on screen
                      return Yes;
                   }

UIGestureRecognizer

UITouch

您还可以使用“大半径公差”来确定大半径的精度

More on Major radius tolerance

答案 1 :(得分:0)

如果您只想检测某些类型的触摸,这是一种可能的解决方案:

选项1

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    touches.forEach { touch in
        switch touch.type {
        case .direct, .pencil:
            handleTouch(for: touch.majorRadius)
        case .indirect:
            // don't do anything
            return
        }
    }
}

func handleTouch(for size: CGFloat) {
    switch size {
    case _ where size > touchThreshold:
        print("do something")
    default:
        print("do something else")
    }
}

选项2

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    touches.filter{($0.type == .direct || $0.type == .pencil || $0.type == .stylus) && $0.majorRadius > touchThreshold}.forEach { touch in
        print("do something")
    }
}

您可以阅读更多的触摸类型here