ボタンの押したている間の強さをリアルタイム検知し描画を変更したいと考えています。
下記のようなカスタムボタンを作成し、クラス内ではforceの値が取得できprintできているのですが、ViewControllerでtapイベントを作成してもなぜか呼ばれず困っています
自作ボタンクラス
Swift
1class ForceButton: UIButton { 2 3 var force: Float = 0.0 4 5 override init(frame: CGRect) { 6 super.init(frame: frame) 7 } 8 9 required init(coder aDecoder: NSCoder) { 10 super.init(coder: aDecoder)! 11 } 12 13 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 14 pressStrength(touches: touches) 15 } 16 17 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 18 pressStrength(touches: touches) 19 } 20 21 22 func pressStrength(touches: Set<UITouch>) { 23 if traitCollection.forceTouchCapability == .available { 24 for touch:UITouch in touches { 25 let strength = Float(touch.force / touch.maximumPossibleForce) 26 force = strength 27 //ここでは正常に取得できている 28 print(force) 29 } 30 } 31 } 32} 33
viewController
allEventsを設定しているのですが、一度もtappedメソッドの中処理が呼ばれない状況です。
どこが間違っているのでしょうか?
@IBOutlet weak var button: UIButton! override func viewDidLoad() { super.viewDidLoad() button.addTarget(self, action: #selector(tapped(_:)), for: .allEvents) } @objc func tapped(_ sender: ForceButton) { print(sender.force) }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/26 01:06