滑动手势添加到UILabel,但它无法正常工作

Ill*_*lep 2 iphone objective-c uilabel uigesturerecognizer ios

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 ;)");
}
Run Code Online (Sandbox Code Playgroud)

所以,这就是:我有一个名为的标签myLabel.当我向右滑动它应该打印NSLog I swiped,但没有任何反应.什么原因?我在这做错了什么?有人可以帮我编辑我的代码以使其工作吗?

Sri*_*aju 10

你不需要添加这个滑动手势UILabel吗?您将它添加到该标签的superview.

改变 - [self.myLabel.superview addGestureRecognizer:swipe];

至 - [self.myLabel addGestureRecognizer:swipe];

更新:另外,作为贾斯汀点,请设定userInteractionEnabledYES的标签,像这样-[self.myLabel setUserInteractionEnabled:YES];


Jus*_*oss 10

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