我想在句柄手势功能中更改squareLabel值,但我真的不知道如何


// This function creates the design of the squares inside of our Game Board View in storyboard
func createSquare (item: UIView) -> UILabel {
let squareLabel = UILabel()

//Setting board view squares size, colors and other attributes
squareLabel.frame = CGRect(x: xY, y: yX, width: square.squareWidth, height: square.squareHeight)

squareLabel.text = ""
squareLabel.textAlignment = .center
squareLabel.layer.cornerRadius = 5
squareLabel.layer.borderWidth = 1

//isUserInteractionEnabled is set to true so that you can tap
squareLabel.isUserInteractionEnabled = true
squareLabel.layer.borderColor = UIColor.lightGray.cgColor

//To add tap recognizers on our squares in Game Board View 
let labelTap = UITapGestureRecognizer(target: self, action: #selector(self.handleGesture))
squareLabel.addGestureRecognizer(labelTap)

return squareLabel
}
@objc func handleGesture(sender: UITapGestureRecognizer? = nil){

if sender?.state == .ended {
print("TAPPEDY TAP")



}
}

您可以从动作方法中的手势识别器获得对标签的引用。

参见此示例:

@objc func handleGesture(sender: UITapGestureRecognizer? = nil){

if sender?.state == .ended {
print("TAPPEDY TAP")
sender?.view?.backgroundColor = UIColor(hue: CGFloat(drand48()), saturation: 1, brightness: 1, alpha: 1)
}
}

最新更新