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
'IT story' 카테고리의 다른 글
| 완료된 작업 만들기 (0) | 2020.07.18 |
|---|---|
| 장치가 터치 스크린인지 감지하는 미디어 쿼리 (0) | 2020.07.18 |
| 글라이드를 사용하여 이미지를 비트 맵으로 다운로드하는 방법은 무엇입니까? (0) | 2020.07.18 |
| POSIX sh에 문자열에 다른 문자열이 포함되어 있는지 어떻게 알 수 있습니까? (0) | 2020.07.18 |
| MySQL에있는 테이블 수를 계산하는 쿼리 (0) | 2020.07.18 |