2017-02-23 30 views
0

我被25.的UIButton接受1個手指2的手指敲擊事件

保持遊戲得分,其中,所述遊戲點遞增/遞減的軌道我使用UIButton s到顯示在按鈕上的分數標籤,以1個手指的輕擊手​​勢將分數增加25,並以2個手指的輕擊手​​勢將分數遞減25。

我找不到更加模塊化的方式來編寫此代碼 ,每個按鈕可重複使用的唯一功能是:

func setButtonTitleAndIncrement(index: Int, button: UIButton) -> Int { 
    var index = index 
    index += 25 
    button.setTitle(String(index), for: .normal) 
    return index 
} 

func setButtonTitleAndDecrement(index: Int, button: UIButton) -> Int { 
    var index = index 
    index -= 25 
    button.setTitle(String(index), for: .normal) 
    return index 
} 

但是對於每個按鈕,除了指定特定的按鈕和方法外,我必須使用相同的代碼,但我無法找出解決方法。 我喜歡採取一般indexbutton至少。有任何想法嗎?

var index1 = 0 
var index2 = 0 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let oneFingerTapButtonTeam1 = UITapGestureRecognizer(target: self, action: #selector(incrementScoreTeam1)) 
    oneFingerTapButtonTeam1.numberOfTouchesRequired = 1 
    buttonTeam1.addGestureRecognizer(oneFingerTapButtonTeam1) 

    let twoFingerTapButtonTeam1 = UITapGestureRecognizer(target: self, action: #selector(decrementScoreTeam1)) 
    twoFingerTapButtonTeam1.numberOfTouchesRequired = 2 
    buttonTeam1.addGestureRecognizer(twoFingerTapButtonTeam1) 

    let oneFingerTapButtonTeam2 = UITapGestureRecognizer(target: self, action: #selector(incrementScoreTeam2)) 
    oneFingerTapButtonTeam2.numberOfTouchesRequired = 1 
    buttonTeam2.addGestureRecognizer(oneFingerTapButtonTeam2) 

    let twoFingerTapButtonTeam2 = UITapGestureRecognizer(target: self, action: #selector(decrementScoreTeam2)) 
    twoFingerTapButtonTeam2.numberOfTouchesRequired = 2 
    buttonTeam2.addGestureRecognizer(twoFingerTapButtonTeam2) 
} 

func incrementScoreTeam1() { 
    print("1 tapped") 
    let ind = setButtonTitleAndIncrement(index: index1, button: buttonTeam1) 
    index1 = ind 
} 

func incrementScoreTeam2() { 
    print("2 tapped") 
    let ind = setButtonTitleAndIncrement(index: index2, button: buttonTeam2) 
    index2 = ind 
} 

func decrementScoreTeam1() { 
    print("1 Two tapped") 
    let ind = setButtonTitleAndDecrement(index: index1, button: buttonTeam1) 
    index1 = ind 
} 

func decrementScoreTeam2() { 
    print("2 Two tapped") 
    let ind = setButtonTitleAndDecrement(index: index2, button: buttonTeam2) 
    index2 = ind 
} 
+0

爲什麼你堅持有自帶的試圖超越事物的UIButton的,所有的併發症:沿線的 的東西嗎?只需使用標籤和設計創建一個自定義UIView子類,並簡單地將手勢附加到它。不需要所有的模糊。 – 2017-02-23 21:35:15

+0

@Sneak我沒有堅持任何事情,發佈問題以更好的方式,所以你的建議對我有意義。我剛開始時用UIButton認爲可能是最好的,但現在我正在重新思考。謝謝! – SRMR

+0

我明白了,帶着一個UIView子類,你將從頭開始擁有如此簡單的定製,而不是覆蓋東西並找到我建議的限制。 GL。 – 2017-02-23 21:56:56

回答

2

如何繼承UIButton,然後添加索引作爲按鈕的屬性。

import UIKit 

@IBDesignable class BaseButton: UIButton { 

    var index = 0 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     setup() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setup() 
    } 

    override func prepareForInterfaceBuilder() { 
     super.prepareForInterfaceBuilder() 
     setup() 
    } 

    func setup() { 
     let oneFingerTapButtonTeam1 = UITapGestureRecognizer(target: self, action: #selector(incrementScoreTeam)) 
     oneFingerTapButtonTeam1.numberOfTouchesRequired = 1 
     addGestureRecognizer(oneFingerTapButtonTeam1) 

     let twoFingerTapButtonTeam1 = UITapGestureRecognizer(target: self, action: #selector(decrementScoreTeam)) 
     twoFingerTapButtonTeam1.numberOfTouchesRequired = 2 
     addGestureRecognizer(twoFingerTapButtonTeam1) 
    } 

    func incrementScoreTeam() { 
     let ind = setButtonTitleAndIncrement(index: index, button: self) 
     index = ind 
    } 

    func decrementScoreTeam() { 
     let ind = setButtonTitleAndDecrement(index: index, button: self) 
     index = ind 
    } 

    func setButtonTitleAndIncrement(index: Int, button: UIButton) -> Int { 
     var index = index 
     index += 25 
     button.setTitle(String(index), for: .normal) 
     return index 
    } 

    func setButtonTitleAndDecrement(index: Int, button: UIButton) -> Int { 
     var index = index 
     index -= 25 
     button.setTitle(String(index), for: .normal) 
     return index 
    } 
} 
+0

這是有道理的,謝謝你的整個答案! – SRMR