#ios #swift
#iOS #быстрый
Вопрос:
// 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")
}
}
Ответ №1:
Вы можете получить ссылку на свою метку из средства распознавания жестов в вашем методе действия.
Смотрите этот пример:
@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)
}
}