1

我已将 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为是。

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

4

3 回答 3

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.
于 2013-08-21T07:15:59.620 回答
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
}
于 2013-08-21T07:50:40.733 回答
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
        }
    } 
}
于 2013-08-21T07:15:25.010 回答