Ios 长按手势不起作用

Ios 长按手势不起作用,ios,objective-c,iphone,swift,ios8,Ios,Objective C,Iphone,Swift,Ios8,使用此方法时长按手势对我无效(void)长按:(uilongpressurerecognizer*)。当我长按标签时,不会调用该手势 - (void)viewDidLoad { [super viewDidLoad]; array =[NSMutableArray arrayWithObjects:@"hello",@"we",@"Are",@"Swift", nil]; int ypoint = 60; for (int i=0; i<[array co

使用此方法时长按手势对我无效
(void)长按:(uilongpressurerecognizer*)
。当我长按标签时,不会调用该手势

 - (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;
}
-(void)viewDidLoad{
[超级视图下载];
array=[NSMutableArray arrayWithObjects:@“hello”、@“we”、@“Are”、@“Swift”、nil];
int-ypoint=60;

对于(int i=0;i将
userInteractionEnabled=true
属性设置为标签。

只要可以识别按键,就可以尝试此选项

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

  }
试试这个

array =[NSMutableArray arrayWithObjects:@"hello",@"we",@"Are",@"Swift", nil];
longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpressed:)];
longPressGesture.minimumPressDuration = 0.6;
longPressGesture.delegate = self;

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];
    label.backgroundColor  = [UIColor redColor];
    [label setUserInteractionEnabled:YES];
    [label addGestureRecognizer:longPressGesture];

    ypoint   = ypoint +70;
}
array=[NSMutableArray数组,其对象为:@“hello”、@“we”、@“Are”、@“Swift”、nil];
LongPress手势=[[UILongPressGestureRecognitizer alloc]initWithTarget:self action:@selector(longpressed:)];
LongPress手势。minimumPressDuration=0.6;
longpresssignate.delegate=self;
int-ypoint=60;

对于(inti=0;i我试过我的Xcode,我发现

label=[[UILabel alloc]initWithFrame:CGRectMake(100,ypoint,300200)];
您的定义有一些问题,这里您应该定义一个新标签,而不是使用全局变量。 换成这个


UILabel*newLabel=[[UILabel alloc]initWithFrame:CGRectMake(100,ypoint,300,200)];

根据您上次的评论,当您单击标签时,您得到了错误的标签

这是因为您对所有标签都使用了全局对象
ui长按gestureignizer
recognizer

您需要为所有标签添加单独的手势识别器,每个标签都是唯一的

因此,在创建标签时创建本地手势识别器,因此您的代码现在将是:

for (int i=0; i<[array count]; i++) 
{
    // Label creation code...

    UILongPressGestureRecognizer *longPressGesture =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longpressed:)];
   // gesture settings...

   // Add UILongPressGestureRecognizer for each label.
   [label addGestureRecognizer:longPressGesture]
}

for(int i=0;i为什么要在它上面添加标签。只注册最后一个的手势识别方法。尝试点击视图底部。将颜色更改为红色,然后按住底部区域即可

label.backgroundColor =[UIColor redColor];

如果要将手势识别添加到所有标签中,请将其放入for循环中。但是,为什么要使用标签,请使用按钮来避免复杂性。

您是否尝试调试它?它是否至少触发了该方法或什么?是的,我正在调试它不是一次性调用它意味着不会触发该行为。您可能可以n基于此查找问题。单击标签时,我得到了错误的标签。当我单击“你好”时,他们为两者都给出了标签1,而当我们单击“是”和“斯威夫特”时,他们给了我标签3。它们采用了错误的数组索引。请检查我的答案,您只注册了最后一个标签的手势识别工作,但没有显示正确的元素,它们仅显示了U las元素只显示正确的元素是什么意思?只显示最后一个元素?这意味着标签的标签设置不正确。现在的确切问题是什么?请具体明确,让我们清楚地知道问题。在这里发布屏幕截图,以便我能帮到你。单击标签时,我得到了错误的标签。当我单击“你好,我们”时当我们点击are和swift时,他们给了我标签3。如果数组的索引错误,这意味着标签出错,请尝试将debug设置为for(int i=0;i