长按手势不工作

Sau*_*pta 3 iphone objective-c ios 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;
}
Run Code Online (Sandbox Code Playgroud)

Ash*_* P. 7

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