Question

In my Application, I have a UIScrollView in which I'm putting UIButtons dynamically. I gave tags to all the button's in UIScrollView. I need Pan Gesture on each button in UIScrollView. But the problem is that only the last button is getting Pan Gesture and not all the buttons.

My Code:

for (int i =0; i<[arrayForTitles count]; i++)
{       
    view = [[UIView alloc] init];
    [view setBackgroundColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.85]];
    view.tag=i;
    view.frame = CGRectMake(0, varForTitleViewHeight, scrView.frame.size.width, 50);
    [scrView addSubview:view];

    //************** view when pan gesture done  ********************

    viewForPanGesture = [[UIView alloc] init];
    [viewForPanGesture setBackgroundColor:[UIColor colorWithRed:0.0/255.0 green:161.0/255.0 blue:255.0/255.0 alpha:1.0]];
    viewForPanGesture.tag=i;
    viewForPanGesture.frame = CGRectMake(100, varForTitleViewHeight, 151, view.frame.size.height);
    [scrView addSubview:viewForPanGesture];

    btnOnScrollTitle = [UIButton buttonWithType:UIButtonTypeCustom];
    btnOnScrollTitle.frame = CGRectMake(0,varForTitleViewHeight,scrView.frame.size.width, 50);
    [btnOnScrollTitle setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    btnOnScrollTitle.tag = i;
    [btnOnScrollTitle addTarget:self action:@selector(blackViewMethod:) forControlEvents:UIControlEventTouchUpInside];
    [scrView addSubview:btnOnScrollTitle];

    panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [panGesture setDelegate:self];
    [panGesture setMaximumNumberOfTouches:1];
    [btnOnScrollTitle addGestureRecognizer:panGesture];

    varForTitleViewHeight = varForTitleViewHeight+51;
}

Now, how to give each object of the UIScrollView the Pan Gesture, and not only the last object.

Any idea, code, tutorial, or link will be great help...

Was it helpful?

Solution

there is no error in code and don't need any modification.you just access them with wrong way. here the code for it.

-(void)handlePan:(UIGestureRecognizer *) sender
 {
   if ((UIButton *)[sender.view viewWithTag:0])   //where  no. '0' tag of button.;
      {
         // code it
      }
   if ((UIButton *)[sender.view viewWithTag:1])   
      {
         // code it
      }


 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top