2

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

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

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

4

2 回答 2

6

试试这个..它对我有用

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];
}
于 2013-06-11T10:23:02.120 回答
1

相反,你可以做一件事将所有标签放在一个数组中myLabelArray

NSArray *myLabelArray;

然后添加以下代码。每次它都会创建一个新的手势识别器实例。

for (UILabel *myLabel in myLabelArray) {
    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(handlePan:)] ;
    [myLabel addGestureRecognizer:gesture];
}
于 2013-06-11T09:46:54.250 回答