ios - 为多个标签添加单个手势识别器

时间:2013-06-11 09:40:55

标签: iphone ios objective-c uigesturerecognizer

我在滚动视图中创建动态标签,我想为所有这些动态生成的标签添加单个手势识别器。    我正在创建以下手势

UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
                                    initWithTarget:self
                                    action:@selector(handlePan:)] ;

现在我想将此手势添加到多个标签中。是否可以为动态创建的标签添加相同的手势?

2 个答案:

答案 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];
}