[英]Button as Parameter in Swipe Gesture Action in Swift
我剛剛開始在Swift中使用滑動手勢。 我正在嘗試將它們與按鈕一起使用:當用戶在按鈕上滑動時,應該執行一項操作。
在我的ViewController類的viewDidLoad()
,我得到了:
let leftSwipeButton = UISwipeGestureRecognizer(target: self, action: "leftSwipeButtonAction")
leftSwipeButton.direction = .Left
myFirstButton.addGestureRecognizer(leftSwipeButton)
mySecondButton.addGestureRecognizer(leftSwipeButton)
myThirdButton.addGestureRecognizer(leftSwipeButton)
myFirstButton
, mySecondButton
和myThirdButton
是按鈕( UIButton
)。
在與viewDidLoad()
相同的級別上,我定義了操作:
func leftSwipeButtonAction() {
// here the .backgroundColor of the button that was swiped is supposed to be set to UIColor.yellowColor()
}
因為我想對多個按鈕使用具有相同功能的leftSwipeButtonAction()
,所以我不想為每個按鈕都編寫一個函數,而是將作為參數滑動的UIButton
傳遞給leftSwipeButtonAction()
。 有沒有辦法做到這一點?
您只能發送UITapGestureRecognizer本身作為選擇器上的參數。 您必須在選擇器名稱后輸入:
let leftSwipeButton = UISwipeGestureRecognizer(target: self, action: "leftSwipeButtonAction:")
func leftSwipeButtonAction(recognizer:UITapGestureRecognizer) {
//You could access to sender view
print(recognizer.view?)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.