0

如果uiscrollview上有一个uiscrollview和多个子view。如何知道用户触摸的位置,即特定视图或滚动视图(空白)

4

2 回答 2

5

使用这种方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        NSLog(@"View touched: %@", touch.view);
}

每次手指触摸屏幕时都会调用此方法,并且触摸知道它触摸的是哪个视图。

编辑:它不会在 UIScrollView 上工作,因为滚动视图本身会获得触摸,请检查:

在 UIScrollView 中滚动时未调用 touchesBegan 方法

如何在 UIScrollView 中启用触摸?

于 2013-10-22T09:20:45.470 回答
3

您有 2 种可能性来检测- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;滚动视图中的触摸:

首先,在你的 viewController 中实现这个方法,并在这个 viewController 上添加滚动视图。

其次,我建议:创建一个从 UIScrollView 继承的自定义类,如下所示:.h:

@interface CustomScrollView : UIScrollView 

@end

米:

#import "CustomScrollView.h"


@implementation CustomScrollView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.nextResponder touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    if(!self.dragging){
        [self.nextResponder touchesMoved:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.nextResponder touchesEnded:touches withEvent:event];
}

@end

在你的 vc 中:

...

CustomScrollView* customScrollView = [[CustomScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        NSLog(@"View touched: %@", touch.view);
}
于 2013-10-22T09:50:34.740 回答