Ios 我如何让我的应用程序忽略特定的触摸?

Ios 我如何让我的应用程序忽略特定的触摸?,ios,swift,uitouch,Ios,Swift,Uitouch,我的iPad应用程序需要能够完全消除特定的触摸,即来自手指或手写笔的触摸和来自手掌的触摸。该视图启用了多点触控,因此我可以分别分析每个触控 目前,我可以使用majorRadius属性区分这些触摸,但我不确定如何排除较大的触摸,即大于我确定阈值的触摸 let touchThreshold : CGFloat = 21.0 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { fo

我的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
        }
    }
}
let touchThreshold:CGFloat=21.0
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
接触{
如果touch.majorRadius>touch阈值{
//解除触摸(???)
}否则{
//在屏幕上绘制触摸屏
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
接触{
如果touch.majorRadius>touch阈值{
//解除触摸(???)
}否则{
//在屏幕上绘制触摸屏
}
}
}

您可以改用此方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch;
您可以在其中检查手势的类型,例如,如果手势识别器的类型为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;
                   }

也可以使用大半径公差来确定大半径的精度


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

选择1

override func touchsbegind(touch:Set,带有事件:UIEvent?){
touch.forEach{touch in
开关触摸式{
案例.直接,.铅笔:
手触式(适用于:触摸,majorRadius)
案例.间接:
//什么都不要做
返回
}
}
}
func handleTouch(尺寸:CGFloat){
开关大小{
案例uu其中大小>触摸阈值:
打印(“做某事”)
违约:
打印(“做其他事情”)
}
}
选择2

override func touchsbegind(touch:Set,带有事件:UIEvent?){
touch.filter{($0.type==.direct | |$0.type==.pencil | |$0.type==.stylus)和&$0.majorRadius>touchThreshold}.forEach{touch-in
打印(“做某事”)
}
}

你可以阅读更多的触摸类型。

我不知道你说的“解除触摸”是什么意思。您已经有了一个if语句来区分触摸,并且只有在您得到想要的触摸时才进行操作。“你想忽略的触摸有什么问题?我不知道这样的触摸类型已经存在差异,”阿德里安说。这确实很有帮助。谢谢。如果你没有,我强烈推荐Dash,一个离线浏览文档的软件包。它支持多种语言。
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")
    }
}
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")
    }
}