【问题标题】:detecting touch on a button inside a scroll view in ios在 ios 的滚动视图内检测按钮的触摸
【发布时间】:2013-12-05 11:33:19
【问题描述】:

我有一个带有自定义单元格的表格视图。在单元格内部有一个水平滚动的滚动视图。在表格视图单元格上添加了一个按钮。但是按钮没有通过触摸响应。按钮没有添加到滚动条上视图。单击此按钮时,我的任务是更改其后面的滚动视图的偏移量,就像下一个按钮一样。如何检测触摸?这是我将内容添加到滚动视图的代码。

 for(int i=0;i<[exerciseArrayForeachSection count];i++){

        Exercise *data = [exerciseArrayForeachSection objectAtIndex:i];

        [_customCell.exTitleLabel setText:data.exName];
         if([data.exImage length] < 1){
             [[AsyncImageLoader sharedLoader] cancelLoadingURL:_customCell.exThumbImageView.imageURL];
         }
      else  if ([data.exImage length] > 0)
        {
            int intExerId = [data.exImage intValue];
            NSString *fullPathString = [NSString stringWithFormat:@"%@%d/%@",EXER_URL,intExerId,data.exImage];
            NSURL* aURL = [NSURL URLWithString:fullPathString];


            _customCell.exThumbImageView.imageURL = aURL;
            imageUrlDetails=fullPathString;
        }

     [_customCell.scroll addSubview:_customCell.excerciseDetailsView];


         _customCell.scroll.contentOffset=CGPointMake(_customCell.scroll.contentOffset.x+320, _customCell.scroll.contentOffset.y);

        }

    [_customCell addSubview:_customCell.scroll];
     [_customCell.scroll bringSubviewToFront:_customCell.nextBtn];


        }

从自定义单元格视图中调用按钮单击的方法,例如:

-(IBAction)nextClickActn:(id)sender{
   [[self delegate]nextButtonAction:self];}

在表格视图控制器中,

- (void)  nextButtonAction:(id)sender {

    UITableViewCell *cell = (UITableViewCell *)sender;
    _customCell=(CustomCellFor_Dashboard*)cell;


   if ( _customCell.scroll.contentOffset.x <= _customCell.scroll.contentSize.width ) {
       CGRect frame;
       frame.origin.x = _customCell.scroll.contentOffset.x +_customCell.scroll.frame.size.width;
       frame.origin.y = 0;
       frame.size = _customCell.scroll.frame.size;
       [_customCell.scroll scrollRectToVisible:frame animated:YES];
      // pageControlBeingUsed = YES;
   }

}

【问题讨论】:

  • 你在按钮中添加了目标方法吗?
  • @keyurbhalodiya 编辑了 question.target 方法正在从 tableview 单元类调用,该方法在上面给出
  • [btn addTarget:self action:@selector(nextButtonAction :) forControlEvents:UIControlEventTouchUpInside];
  • 问题是 nextButtonAction 被称为从 snother 视图的委托方法。
  • ok 然后设置 if 条件是否从 uitableview 单元格调用。使用 1 个布尔变量。

标签: ios uitableview uiscrollview uibutton


【解决方案1】:

试试这个, 将此实现到 cellForRowAtIndexPath 方法中:

UIButton *accessoryView = [[UIButton alloc] initWithFrame:CGRectMake(190, 5, 40, 41)];

accessoryView.tag = indexPath.row;
             [accessoryView addTarget: self action:@selector(yourMethodWhichUWantToCall:)forControlEvents:UIControlEventTouchUpInside];
             cell.accessoryView = accessoryView;

然后在该方法中实现以下代码:

UIButton *accessoryView = (UIButton *)sender;

UITableViewCell *cell;

if(isIOS7)
{
   cell = (UITableViewCell *)accessoryView.superview.superview;
}
else
{
   cell = (UITableViewCell *)accessoryView.superview;
}

【讨论】:

  • 但它甚至没有调用这个方法 -(IBAction)nextClickActn:(id)sender;点击
【解决方案2】:

我认为你需要使用这条线

self.button.userInteractionEnabled = YES;

或者你可以使用。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];

// prevents the scroll view from swallowing up the touch event of child buttons
tapGesture.cancelsTouchesInView = NO;    

[pageScrollView addGestureRecognizer:tapGesture];

【讨论】:

  • 按钮交互已启用,在添加滚动视图之前,触摸检测正常。
  • button.clipToBounds = YES - 试试这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多