Swift:如何使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") } }
你有没有尝试在tripDetails
标签上设置userInteractionEnabled
为true
? 这应该工作。
Swift 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) } func tapFunction(sender:UITapGestureRecognizer) { print("tap working") } }
Swift 3更新
yourLabel.isUserInteractionEnabled = true
您需要启用该标签的用户交互…..
例如
yourLabel.userInteractionEnabled = true
SWIFT 4.0更新
@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") }
对于SWIFT 3.0,你也可以改变手势长按持续时间
label.isUserInteractionEnabled = true let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(userDragged(gesture:))) longPress.minimumPressDuration = 0.2 label.addGestureRecognizer(longPress)
- targetContentOffsetForProposedContentOffset:withScrollingVelocity无子类化UICollectionViewFlowLayout
- 为什么我的UIButton.tintColor不起作用?
- 在UIScrollView中使用自动布局的UITableView
- 核心数据 – 无法加载path上的优化模型
- 如何从iPhone上的API显示HTML文本?
- Watchkit扩展 – 找不到匹配的configuration文件
- 将坐标转换为城市名称?
- 需要一个开发团队。 在项目编辑器中select一个开发团队。SDK“iOS 10.0”中的产品types“应用程序”需要代码签名
- 从父iOS访问容器视图控制器