IT story

UILabel을 클릭 가능하게 만드는 방법은 무엇입니까?

hot-time 2020. 7. 18. 10:00
반응형

UILabel을 클릭 가능하게 만드는 방법은 무엇입니까?


UILabel을 클릭 가능하게 만들고 싶습니다.

나는 이것을 시도했지만 작동하지 않습니다.

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        let tap = UITapGestureRecognizer(target: self, action: Selector("tapFunction:"))
        tripDetails.addGestureRecognizer(tap)
    }

    func tapFunction(sender:UITapGestureRecognizer) {
        print("tap working")
    }
}

라벨 설정 isUserInteractionEnabled시도 했습니까 ? 이 작동합니다.truetripDetails


스위프트 3 업데이트

바꾸다

Selector("tapFunction:")

#selector(DetailViewController.tapFunction)

예:

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...

        let tap = UITapGestureRecognizer(target: self, action: #selector(DetailViewController.tapFunction))
        tripDetails.isUserInteractionEnabled = true
        tripDetails.addGestureRecognizer(tap)
    }

    @objc
    func tapFunction(sender:UITapGestureRecognizer) {
        print("tap working")
    }
}

SWIFT 4 업데이트

 @IBOutlet weak var tripDetails: UILabel!

 override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action: #selector(GameViewController.tapFunction))
    tripDetails.isUserInteractionEnabled = true
    tripDetails.addGestureRecognizer(tap)
}

@objc func tapFunction(sender:UITapGestureRecognizer) {

    print("tap working")
}

스위프트 3 업데이트

yourLabel.isUserInteractionEnabled = true

스위프트 5

@liorco비슷 하지만 @objc@IBAction 으로 바꿔야 합니다.

class DetailViewController: UIViewController {

    @IBOutlet weak var tripDetails: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        ...

        let tap = UITapGestureRecognizer(target: self, action: #selector(DetailViewController.tapFunction))
        tripDetails.isUserInteractionEnabled = true
        tripDetails.addGestureRecognizer(tap)
    }

    @IBAction func tapFunction(sender: UITapGestureRecognizer) {
        print("tap working")
    }
}

이것은 Xcode 10.2에서 작동합니다.


해당 라벨의 사용자 상호 작용을 활성화해야합니다 .....

예를 들어

yourLabel.userInteractionEnabled = true


좋고 편리한 솔루션 :

ViewController에서 :

@IBOutlet weak var label: LabelButton!

override func viewDidLoad() {
    super.viewDidLoad()

    self.label.onClick = {
        // TODO
    }
}

이것을 ViewController 또는 다른 .swift 파일 (예 : CustomView.swift)에 배치 할 수 있습니다.

@IBDesignable class LabelButton: UILabel {
    var onClick: () -> Void = {}
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        onClick()
    }
}

스토리 보드에서 레이블을 선택하고 필드 클래스의 "Identity Inspector"에있는 오른쪽 분할 창에서 LabelButton을 선택하십시오.

Label Attribute Inspector "User Interaction Enabled"에서 활성화하는 것을 잊지 마십시오


For swift 3.0 You can also change gesture long press time duration

label.isUserInteractionEnabled = true
let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(userDragged(gesture:))) 
longPress.minimumPressDuration = 0.2
label.addGestureRecognizer(longPress)

Pretty easy to overlook like I did, but don't forget to use UITapGestureRecognizer rather than UIGestureRecognizer.


As described in the above solution you should enable the user interaction first and add the tap gesture

this code has been tested using

Swift4 - Xcode 9.2

yourlabel.isUserInteractionEnabled = true
yourlabel.addGestureRecognizer(UITapGestureRecognizer(){
                //TODO 
            })

참고URL : https://stackoverflow.com/questions/33658521/how-to-make-a-uilabel-clickable

반응형