ios - 长按手势不起作用

标签 ios objective-c iphone swift ios8

当我使用此方法 (void)longpressed:(UILongPressGestureRecognizer *) 时,长按手势对我不起作用。当我长按标签时不会调用手势。

 - (void)viewDidLoad {
    [super viewDidLoad];
    array =[NSMutableArray arrayWithObjects:@"hello",@"we",@"Are",@"Swift", nil];
    int ypoint = 60;
    for (int i=0; i<[array count]; i++) {
        label=[[UILabel alloc]initWithFrame:CGRectMake(100, ypoint, 300, 200)];
        label.backgroundColor =[UIColor clearColor];
        label.text =[array objectAtIndex:i];
        [label setTag:i];
        [self.view addSubview:label];
        ypoint = ypoint +70;
    }
    [label setUserInteractionEnabled:YES];
    longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpressed:)];
    longPressGesture.minimumPressDuration = 0.6;
    longPressGesture.delegate = self;
    [label addGestureRecognizer:longPressGesture];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)longpressed:(UILongPressGestureRecognizer *)gesture{
     if (gesture.state == UIGestureRecognizerStateBegan) {
            UILabel *myLabel= (UILabel *)gesture.view ;
            NSInteger myLabelTag =[myLabel tag];
            NSString *nameString=[array objectAtIndex:myLabelTag];
            NSLog(@"%@",nameString);                
     }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  {
    return YES;
}

最佳答案

userInteractionEnabled = true 属性设置为您的标签。

关于ios - 长按手势不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28038267/

相关文章:

iphone - 如何在 MKMapView 上的用户当前位置添加 UIImageView

ios - 如果 'Accessibility Identifier' 用于 UI 自动化测试,为什么它包含在可本地化资源中?

ios - 在 iOS13 中以模式方式显示插页式广告时遇到问题

objective-c - iOS:TableView + 带有图像的部分中标题的标题

objective-c - 使用 Facebook Scrumptious 应用示例代码

ios - 如何在关闭 ViewController 后继续倒计时

ios - UIActivityViewController 分享图片到微信不起作用

iphone - 如何从我的 iPhone App 文档目录中选择所有图像

ios - 如何在iOS上实现 "vertical reveal"的效果,最好是用UITableView

ios - 如何在 UIWebView 中打开西里尔域名?