簡體   English   中英

我想更改 handleGesture func 中的 squareLabel 值,但我真的不知道如何

[英]I want to change squareLabel values in handleGesture func, but I really don't know how to

// 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")
        
        
        
    }
}

您可以從動作方法中的手勢識別器中獲取對 label 的引用。

看這個例子:

    @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)
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM