2

My iPad app needs to be able to completely dismiss specific touches i.e. those that come from a finger or stylus and not from, say, the palm of your hand. The view has multi-touch enabled so that I analyse each touch separately.

I can currently differentiate between these touches using the majorRadius attribute but I'm not sure how I might be able to dismiss the larger touches i.e. those greater than my decided threshold.

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
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I'm not sure what you mean by "dismiss the touch". You already have an if statement to differentiate the touches and only act when you get one you want. What problem are you having with the touches you want to ignore? – WolfLink Jan 25 '19 at 03:39

2 Answers2

0

You can use this method instead

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

where you can check the type of gesture,for example if the gesturerecognizer is of type UITapGestureRecognizer

You can check if the touch major radius is within your threshold limit and if its within your threshold you pass true else you pass 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

You can also use Major radius tolerance to determine the accuracy of the major radius

More on Major radius tolerance

Pooja Kamath
  • 1,290
  • 1
  • 10
  • 17
0

If you only want to detect certain types of touches, here's a possible solution:

Option 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")
    }
}

Option 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")
    }
}

You can read more touch types here.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • I didn't realise differentiations existed already for touch types like that, @Adrian. That's very helpful indeed. Thank you. – Jess McKenzie Jan 29 '19 at 00:09
  • 1
    If you don't have it, I highly recommend Dash, a software package for navigating documentation offline. It supports multiple languages. – Adrian Jan 29 '19 at 17:57