ios - 我们可以从 ios 中的 ScrollView 中获取触摸的 subview 属性吗

标签 ios uiscrollview subview

我已将 20 个 subview 逐行添加到 scrollview

yPos=0;
    for (int i=0; i<24; i++) {

    UIView *timeView=[[UIView alloc]initWithFrame:CGRectMake(71, yPos, 909, 60)];
    timeView.userInteractionEnabled=TRUE;
    timeView.exclusiveTouch=YES;
    timeView.tag=i;
    NSLog(@"sub vieww tag=:%d",timeView.tag);
    timeView.backgroundColor=[UIColor whiteColor];
    UILabel *lbltime=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 60)];
    lbltime.text=@"VIEW HERE";
    lbltime.textColor=[UIColor grayColor];
  //  [timeView addSubview:lbltime];
    [scrlView addSubview:timeView];

    yPos=yPos+61;
}

现在,每当我点击一个 subview 时,我都不会获得点击的 subview 属性。

喜欢坐标。它正在给父 View 坐标

我将 subview UserInteractionEnabled 启用为是。

谁能告诉我如何获取点击的 subview 坐标和标签值?

最佳答案

不要从 UIScrollView 继承子类,这正是有手势识别器的原因。此外,请勿向每个 View 添加单独的手势识别器。

向您的 ScrollView 添加一个手势识别器,当它被点击时使用触摸的 x,y 值来计算哪个 View 被点击。 您需要做一个小计算:(点击的 y 值 + UIScrollView y 偏移量)/60。 60是每个 View 的高度。这应该返回被点击 View 的索引。

编辑:

代码示例:

- (void)viewTapped:(UIGestureRecognizer*)recognizer
{
    CGPoint coords = [recognizer locationInView:recognizer.view];
    int clickedViewIndex = (self.offset.y + coords.y) / 60;

    // now clickedViewIndex contains the index of the clicked view
}

关于ios - 我们可以从 ios 中的 ScrollView 中获取触摸的 subview 属性吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18350795/

相关文章:

objective-c - UIScrollView 不会调整高度以适应 subview

ios - UIScrollView 委托(delegate)方法 scrollViewWillEndDragging : not working?

swift - SWRevealViewController 具有实用 View

ios - 无法推断通用参数 'S',对图像使用 clipShape

ios - 更改iframe中视频加载的方向

ios - Swift 3 中的 CGAffineTransformConCat/CGAffineTransformMakeScale

ios - UIScrollView ClipsToBounds 只能垂直应用吗?

ios - xib/uibuttons 添加为 ViewController subview ?

ios - UIView : removeFromSuperview VS hide 的性能

ios - 通知到达时显示来自 AppDelegate 的 View Controller