Reputation: 2406
In Swift 2 I have the touchesBegan
method with Set<UITouch>
. Now, in the method I would like to check whether it was a tap/touch, a 3D touch..
I tried using .phase
on the first UITouch
, but that resulted in nothing. How would I check what kind of touch the user did?
Upvotes: 1
Views: 1251
Reputation: 27620
You cannot check that by looking at touchesBegan
alone. That method is only called when the user first touches the screen. The system cannot know what will happen after that (if the user lifts the finger again, or if he moves it, or if he presses with force etc.).
You also have to have a look at touchesMoved
and touchesEnded
and check what is happening there.
For example:
1) The touch was a Tap if the touches locationInView
property is more or less the same in touchesBegan
and touchesEnded
. The won't be exactly the same because even if the user just taps the screen the finger moves a tiny bit.
2) The touch was a Pan or Swipe if the touches locationInView
property is different in touchesBegan
and touchesEnded
.
3) The touch was a ForceTouch if the touches 'force' property is larger than a certain amount. This is only visible in touchesMoved
. In touchesBegan
the force
property will always be 0
. Of course force
will only be available on devices that offer that feature.
If you want to detect a ForceTouch yourself you can do it like this:
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
if traitCollection.forceTouchCapability == .Available {
if touch.force == touch.maximumPossibleForce {
print("This is a force touch")
}
}
}
Just be aware that this is called continuously as long as the user presses the screen. If you want to execute some code once when you detect a force touch you have take care that you only execute the code once until touchesEnded
has been called.
Upvotes: 1