我在滚动视图中创建动态标签,我想为所有这些动态生成的标签添加单个手势识别器。 我正在创建以下手势
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)] ;
现在我想将此手势添加到多个标签中。是否可以为动态创建的标签添加相同的手势?
答案 0 :(得分:5)
试试这个..为我工作
NSMutableArray *arrayForLabels= [NSMutableArray array];
[arrayForLabels addObject:label];
[arrayForLabels addObject:label1];
// enable touch delivery
label.userInteractionEnabled = YES;
label1.userInteractionEnabled = YES;
for (UILabel *myLabel in arrayForLabels) {
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)] ;
[myLabel addGestureRecognizer:gesture];
}
答案 1 :(得分:1)
相反,你可以做一件事
将所有标签放在数组myLabelArray
NSArray *myLabelArray;
然后添加以下代码。 每次它都会创建一个新的gesturerecognizer实例。
for (UILabel *myLabel in myLabelArray) {
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)] ;
[myLabel addGestureRecognizer:gesture];
}