UILabel 添加了滑动手势,但不起作用
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedGesture:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
swipe.numberOfTouchesRequired = 1;
[self.myLabel.superview addGestureRecognizer:swipe];
- (void)swipedGesture:(UIGestureRecognizer *)recognizer
{
NSLog(@"I swiped ;)");
}
所以,发生的事情是这样的:我有一个名为 myLabel
的标签。当我向右滑动时,它应该打印 NSLog
I swiped
,但什么也没有发生。原因是什么?我在这里做错了什么?有人可以帮我编辑我的代码以使其工作吗?
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedGesture:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
swipe.numberOfTouchesRequired = 1;
[self.myLabel.superview addGestureRecognizer:swipe];
- (void)swipedGesture:(UIGestureRecognizer *)recognizer
{
NSLog(@"I swiped ;)");
}
So, this is what happens: I have a label called myLabel
. and when I swipe right it should print the NSLog
I swiped
, but nothing happens. What's the reason? What have I done wrong here? Could someone help me edit my code to make this work ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要将此滑动手势添加到
UILabel
吗?您正在将其添加到该标签的超级视图中。将 -
[self.myLabel.superview addGestureRecognizer:swipe];
更改为 -
[self.myLabel addGestureRecognizer:swipe];
更新:同样作为 justin 点,请设置
对于标签,将 userInteractionEnabled
更改为YES
,如下所示 -[self.myLabel setUserInteractionEnabled:YES];
Dont you need to add this swipe gesture to the
UILabel
? you are adding it to the superview of that label.change -
[self.myLabel.superview addGestureRecognizer:swipe];
to -
[self.myLabel addGestureRecognizer:swipe];
UPDATE: Also as justin points, please set
userInteractionEnabled
toYES
for the label like so -[self.myLabel setUserInteractionEnabled:YES];
除非您在标签上将
userInteractionEnabled
设置为YES
,否则手势识别器将无法工作。The gesture recognizer won't work unless you set
userInteractionEnabled
toYES
on the label.