UILabel 添加了滑动手势,但不起作用

发布于 2024-12-28 17:05:05 字数 537 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

A君 2025-01-04 17:05:05

您不需要将此滑动手势添加到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 to YES for the label like so - [self.myLabel setUserInteractionEnabled:YES];

雨夜星沙 2025-01-04 17:05:05

除非您在标签上将 userInteractionEnabled 设置为 YES,否则手势识别器将无法工作。

The gesture recognizer won't work unless you set userInteractionEnabled to YES on the label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文