0

I am working on a MIDI Keyboard app for iPad and iPhone. I am already able to send note on/note off MIDI messages, but only with a fixed velocity. (I could give the users a slider to adjust that velocity, but that would not be optimal either.)

I would like detect the velocity of a touch, e.g. how hard the user taps on the screen. (I do not mean the velocity of a swipe gesture, which is of course something completely different. I also don't mean the force of a 3D Touch.)

I am sure that it's possible, even on devices that do not have 3D Touch. Apple use this in their GarageBand app, and it works quite well: when you touch the screen softly, you hear a soft note, and when you hit the screen hard, you get a harder sound.

So I would like to do the same thing in my app. Would anyone have a suggestion on how to realize this? Thanks in advance!

cbjeukendrup
  • 3,216
  • 2
  • 12
  • 28

1 Answers1

1

You can access force of UITouch overriding touchesMoved function in UIKit:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    guard let touch = touches.first else { return }
    // range between 0 and touch.maximumPossibleForce
    print(touch.force)
}
Omer Faruk Ozturk
  • 1,722
  • 13
  • 25
  • Thanks for your reaction! I also saw this force property, but as the documentation says, it's only available for 3D Touch devices... What I need is a way to detect how hard the user *hits* the screen (at the start of the touch), instead of the force with which the user is holding down their finger at the screen. – cbjeukendrup Jul 04 '20 at 17:55
  • 1
    I am not sure but [this](https://stackoverflow.com/questions/5179426/tap-pressure-strength-detection-using-accelerometer) question may help you. – Omer Faruk Ozturk Jul 04 '20 at 19:32