当我使用此方法 (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/