2017-02-23 29 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