我发现创建UILabel比UITextField快得多,计划在我的数据展示应用中大部分时间使用UILabel。
简而言之,我希望允许用户点击UILabel并在我的回调函数中响应。这是否可能?
谢谢。
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
myLabel.userInteractionEnabled = YES;
如果您正在使用Storyboards,可以在Storyboard中完成整个过程而无需编写额外的代码。向Storyboard中添加一个标签,然后向标签添加一个轻击手势。在Utilities面板中,确保为标签选中了“用户交互启用”复选框。从轻击手势(位于Storyboard中您的视图控制器底部),在您的ViewController.h文件中按Ctrl +单击并拖动,创建一个操作。然后在ViewController.m文件中实现操作。
Swift 3.0
为 tempLabel
初始化手势。
tempLabel?.text = "Label"
let tapAction = UITapGestureRecognizer(target: self, action: #selector(self.actionTapped(_:)))
tempLabel?.isUserInteractionEnabled = true
tempLabel?.addGestureRecognizer(tapAction)
操作接收器
func actionTapped(_ sender: UITapGestureRecognizer) {
// code here
}
Swift 4.0
为tempLabel
初始化手势
tempLabel?.text = "Label"
let tapAction = UITapGestureRecognizer(target: self, action:@selector(actionTapped(_:)))
tempLabel?.isUserInteractionEnabled = true
tempLabel?.addGestureRecognizer(tapAction)
操作接收器
func actionTapped(_ sender: UITapGestureRecognizer) {
// code here
}
Swift 2.0:
我将一个NSMutableString添加为sampleLabel的文本,启用用户交互,添加一个点击手势并触发一个方法。
override func viewDidLoad() {
super.viewDidLoad()
let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue], range: NSMakeRange(4, 4))
sampleLabel.attributedText = newsString.copy() as? NSAttributedString
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
tapGesture.numberOfTapsRequired = 1
sampleLabel.userInteractionEnabled = true
sampleLabel.addGestureRecognizer(tapGesture)
}
func tapResponse(recognizer: UITapGestureRecognizer) {
print("tap")
}
如何在UILabel上添加轻拍手势
UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lblClick:)];
tapAction.delegate =self;
tapAction.numberOfTapsRequired = 1;
//Enable the lable UserIntraction
lblAction.userInteractionEnabled = YES;
[lblAction addGestureRecognizer:tapAction];
并评估选择器方法
- (void)lblClick:(UITapGestureRecognizer *)tapGesture {
}
注意:在.h文件中添加UIGestureRecognizerDelegate
Swift版本:
var tapGesture: UITapGestureRecognizer = UITapGestureRecognizer()
然后在viewDidLoad()
中添加以下内容:
let yourLbl=UILabel(frame: CGRectMake(x,y,width,height)) as UILabel!
yourLbl.text = "SignUp"
tapGesture.numberOfTapsRequired = 1
yourLbl.addGestureRecognizer(tapGesture)
yourLbl.userInteractionEnabled = true
tapGesture.addTarget(self, action: "yourLblTapped:")
这在 Xcode 12 和 Swift 5 中非常有效。
let tapAction = UITapGestureRecognizer(target: self,action:#selector(actionTapped(_:)))
userLabel?.isUserInteractionEnabled = true
userLabel?.addGestureRecognizer(tapAction)
像这样的操作方法 -
@objc func actionTapped(_ sender: UITapGestureRecognizer) {
print("tapped")
}
来自Alvin George的Swift 3
override func viewDidLoad() {
super.viewDidLoad()
let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue], range: NSMakeRange(4, 4))
sampleLabel.attributedText = newsString.copy() as? NSAttributedString
let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapResponse))
tapGesture.numberOfTapsRequired = 1
sampleLabel.isUserInteractionEnabled = true
sampleLabel.addGestureRecognizer(tapGesture)
}
func tapResponse(recognizer: UITapGestureRecognizer) {
print("tap")
}
UILabel
并将其添加为子视图到您的按钮中。yourLabel=[Uilabel alloc]init];
yourLabel.frame=yourButtom.Frame;//(frame size should be equal to your button's frame)
[yourButton addSubView:yourLabel]
userInteractionEnabled = true
翻译为“需要设置'用户交互使能'为真”。 - onmyway133