3

how can I measure the 3D touch force applied to a UIButton function? I want to use it for changing the volume of an AVAudioPlayer property in swift.

jesus
  • 69
  • 8

1 Answers1

4

You can use something that recognizes 3D touch for all UIView classes \ subclass

make a custom class

class customButton : UIButton{
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches{
            print("% Touch pressure: \(touch.force/touch.maximumPossibleForce)");
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        print("Touches End")
    }
}

and instead of UIButton Touch Observer (TouchUpInside \ TouchUpInside..) just use this methods above
this will work for all UIView class

class customView : UIView ....//same code as above
Bankim
  • 254
  • 4
  • 14
Daniel Krom
  • 9,751
  • 3
  • 43
  • 44
  • But how can I use this code in an unbutton that has been created in interface builder -Daniel Krom – jesus Feb 06 '16 at 15:54
  • @arshiyaasd create a custom class and at the interface builder select the button then go to Custom Class and select that class https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Art/5_inspector_identity_2x.png – Daniel Krom Feb 06 '16 at 16:20
  • Thanks a lot. It helped me. – jesus Feb 06 '16 at 16:26