为所有UIScrollView的子类赋予Pan Gesture标签

时间:2014-02-21 06:30:36

标签: ios iphone uiview uiscrollview uigesturerecognizer

在我的应用程序中,我有UIScrollView,其中我动态地放置UIButton。我为UIScrollView中的所有按钮添加了标签。 我需要在UIScrollView中的每个按钮上使用Pan Gesture 。但问题是只有最后一个按钮才能获得Pan Gesture 并非所有按钮。

我的代码:

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;
}

现在,如何给UIScrollView Pan Gesture的每个对象,而不仅仅是最后一个对象。

任何想法,代码,教程或链接都会有很大的帮助......

1 个答案:

答案 0 :(得分:0)

代码中没有错误,也不需要任何修改。你只是以错误的方式访问它们。 这里是代码。

-(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
      }


 }