我们可以从ios中的滚动视图中获取触摸的子视图属性

时间:2013-08-21 07:01:03

标签: ios uiscrollview subview

我已将scrollview的20个子视图逐行添加为行

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为是。

任何人都可以告诉我如何获取抽头的子视图坐标和标记值吗?

3 个答案:

答案 0 :(得分:1)

           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.

答案 1 :(得分: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
}

答案 2 :(得分:0)

创建一个扩展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
        }
    } 
}