2

I am creating dynamic labels in scroll view and I want to add single gesture recognizer to all these dynamically generated labels. I am creating gesture as following

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

Now I want to add this gesture to multiple labels. Is it possible to add same gesture to dynamically created labels ?

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
nikBhosale
  • 531
  • 1
  • 5
  • 27

2 Answers2

6

try this..its working for me

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];
}
Rahul
  • 314
  • 5
  • 12
1

Instead u can do one thing Put all your labels in an array myLabelArray

NSArray *myLabelArray;

then add following code. every time it will create a new instance of gesturerecognizer.

for (UILabel *myLabel in myLabelArray) {
    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(handlePan:)] ;
    [myLabel addGestureRecognizer:gesture];
}
Prashant Nikam
  • 2,253
  • 4
  • 17
  • 29