当我使用此方法(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;
}
答案 0 :(得分:7)
将userInteractionEnabled = true
属性设置为您的标签。
答案 1 :(得分:1)
尝试这个,因为长按被识别,
-(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);
}
答案 2 :(得分:0)
试试这个
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;
}
在您的代码中进行更改后,它可以正常工作
答案 3 :(得分:0)
我尝试了我的Xcode,我发现了
label=[[UILabel alloc]initWithFrame:CGRectMake(100, ypoint, 300, 200)];
你的定义有一些问题,在这里你应该定义一个新标签,而不是使用全局变量。
改为这个
UILabel *newLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, ypoint, 300, 200)];
答案 4 :(得分:0)
根据您上次的评论,当您点击标签时,标签会出错。
这是因为您为所有标签使用了全局对象UILongPressGestureRecognizer
识别器。
您需要为所有标签添加单独的手势识别器,这些标签对于每个标签都是唯一的。
因此,在创建标签时创建本地手势识别器,因此您的代码现在将是:
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]
}
答案 5 :(得分:0)
为什么要在其上添加标签。只有最后一个手势识别方法才能被注册。尝试点击视图的底部。将颜色更改为红色,然后按住它将起作用的底部区域。
label.backgroundColor =[UIColor redColor];
如果要将手势识别添加到所有标签,请将其放在for循环中。但是你为什么要使用标签,而是使用按钮来避免复杂化。