【问题标题】:can we get the touched subview properties from scroll view in ios我们可以从ios的滚动视图中获取触摸的子视图属性吗
【发布时间】:2013-08-21 07:01:03
【问题描述】:

我已将 20 个子视图逐行添加到 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;
}

现在,当我点击子视图时,我不会获得点击的子视图属性。

喜欢坐标。它给出了父视图坐标

我将子视图UserInteractionEnabled 启用为是。

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

【问题讨论】:

  • 将点击手势添加到您正在添加的视图中,并为这些视图添加标签,并根据 Tapgetsure 事件方法中的标签识别视图。
  • 我建议您编辑帖子以显示您用于处理水龙头的代码。如果您使用的是轻击手势识别器,它附加到什么 - 子视图或滚动视图或什么?
  • 在 Tapgetsure 事件中如何获得被点击的视图。UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; tap.numberOfTapsRequired = 1; [self.view addGestureRecognizer:tap];在事件中-(void)tap:(UITapGestureRecognizer *)recognizer { ....} 如何获得被点击的视图。我已经为每个子视图设置了标签
  • 我将 SubView 添加到 ScrollView 并将 Scroll 视图添加到父视图
  • UIView *v = Recognizer.view; int tagNum = [v 标签]; tagNum 是你点击视图的标签。

标签: ios uiscrollview subview


【解决方案1】:

不要从UIScrollView 继承,这正是有手势识别器的原因。此外,不要为每个视图添加单独的手势识别器。

将一个手势识别器添加到您的滚动视图中,当它被点击时,使用触摸的 x,y 值来计算点击了哪个视图。 你需要做一个小计算:(y value of the click + UIScrollView y offset) / 60。 60 是每个视图的高度。这应该返回被点击视图的索引。

编辑:

代码示例:

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

【讨论】:

  • Eli ganem 我能解释一下吗
【解决方案2】:
           UIView *v = recognizer.view; 
            int tagNum = [v tag];

            Using the tagNum you can do your further operatins.
        Or v is your object of tapped view.

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; 
tap.numberOfTapsRequired = 1; 
[timeview addGestureRecognizer:tap];

    Add this in for loop only.

【讨论】:

  • 我遇到了另一个问题。如何在滚动视图的子视图上添加视图?
  • [子视图 addSubView:view];
  • 在我的滚动视图中有 20 个子视图。如何在任意两个或三个子视图上添加视图。这些子视图已经是滚动视图的子视图
  • UIView *view=(UIView *)[subview viewWithTag:viewTagNumber];
【解决方案3】:

创建一个扩展 UIScrollView 的类:

例如:

.h file :

@protocol CustomScrollViewDelegate <NSObject>

@optional
// optional becuase we always don't want to interact with ScrollView

- (void) customScrollViewTouchesEnded :(NSSet *)touches withEvent:(UIEvent *)event;

- (void) customScrollViewDidScroll;


@end

@interface CustomScrollView : UIScrollView

@property (weak, nonatomic) id<CustomScrollViewDelegate> touchDelegate;
// delegate was giving error becuase name is already there in UIScrollView 

@end

.m 文件的代码:

#import "CustomScrollView.h"

@implementation CustomScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touchesEnded");
    if (!self.dragging) {
        //NSLog(@"touchesEnded in custom scroll view");
        if (_touchDelegate != nil) {
            if ([_touchDelegate respondsToSelector:@selector(customScrollViewTouchesEnded:withEvent:)]) {
                [_touchDelegate customScrollViewTouchesEnded:touches withEvent:event];
            }
        }
    }

    else {
        // it wouldn't be called becuase at the time of dragging touches responding stops.
        [super touchesEnded:touches withEvent:event];
    }

}

@end

实现这个使用滚动视图的子视图,它会起作用

for (UILabel *label in [customScrollView subviews]) { // change name of table here
    if ([label isKindOfClass:[UILabel class]]) {
        if (label.tag == savedtag) { // use your tag
            // write the code as desired
        }
    } 
}

【讨论】:

    猜你喜欢
    • 2012-06-29
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    相关资源
    最近更新 更多