我在UIViewController
中有多個UIButton
實例,並且我想在按壓任何這些按鈕(一直向下)時執行一些操作,我不想在這裏不知道確切的名詞(強迫接觸也許?)。對UIButton上的3D Touch執行操作的最佳方式
所以當UIButton
的壓力,我想通過振動給觸覺反饋,更改按鈕圖像源,並做一些其他的東西。然後,當壓力被釋放,我要恢復按鈕圖像源到正常狀態,並做一些更多的東西。
什麼是最簡單的方法是什麼?
我是否應該像我自己的自定義UIButton
一樣,或有方法可以覆蓋3D按「按下」和「釋放」。
這是我的自定義的UIButton到目前爲止的代碼。我是否應該通過反覆試驗確定最大力量應該是多少?還有我怎麼改變圖像的在可能的最簡單的方法每個按鈕的來源?
import AudioToolbox
import UIKit
class customButton : UIButton {
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
print("% Touch pressure: \(touch.force/touch.maximumPossibleForce)");
if touch.force > valueThatIMustFindOut {
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
// change image source
// call external function
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("Touches End")
// restore image source
// call external function
}
}
請注意,我是Swift新手,所以我想盡可能使用Xcode中的圖形界面來創建用戶界面。所以我想避免從代碼中創建UI。