문제

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...

도움이 되었습니까?

해결책

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
      }


 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top