2016-01-23 87 views
1

我在視圖中設置了幾個drumpad,它們都連接到drumPadPlay IBAction方法。我想確定發件人按鈕內用戶按下的y軸位置,以調整鼓墊將播放的音量。我正在使用swift。在swift中獲取UIButton的位置

在這被標記爲重複問題之前,我引用了這個問題,該解決方案並不適用於當前版本的swift,或者我不知道該如何回答「buttonView」。 Detect what is the location of the tap/press inside UIButton inside sender action method

@IBAction func drumPadPressed(sender: BNDrumPad, event: UIEvent) { 

    //...code for function here 

    // get any touch on the buttonView 
    if let touch = event.touchesForView(sender.)?.anyObject() as? UITouch { 
     // print the touch location on the button 
     println(touch.locationInView(buttonView)) 
    } 

    //...more code for function here 
} 

這裏是我的代碼 - 錯誤是「使用未解決的標識符‘buttonView’」 我不知道用什麼buttonView。我曾嘗試sender.superview!並通過發件人的屬性/功能列表進行搜索,並找不到任何看起來會有所幫助的內容。

+1

您鏈接到的答案是正確的。也許你可以編輯你的問題,以顯示你嘗試的代碼和你有錯誤/問題。 – Paulw11

+0

我編輯了問題以包含代碼示例。我有一種感覺,這是一個簡單的答案,另一個答案假設它將知道什麼來替換buttonView,但我無法弄清楚。 – evansjohnson

回答

1

senderBNDrumPad這是(大概)UIView的子類,所以在這種情況下,您將只使用sender。在你連接的答案中,sender參數被定義爲AnyObject,因此他們必須首先將其轉換爲UIView

@IBAction func drumPadPressed(sender: BNDrumPad, event: UIEvent) { 

    //...code for function here 

    // get any touch on the buttonView 
    if let touch = event.touchesForView(sender)?.anyObject() as? UITouch { 
     // print the touch location on the button 
     println(touch.locationInView(buttonView)) 
    } 

    //...more code for function here 
}