如果存在uiscrollview和uiscrollview上有多个子视图。如何知道用户触摸的位置,即特定视图或滚动视图(空白)
最佳答案
在scrollView中有两种可能性可以通过- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
检测触摸:
首先,在您的viewController中实现此方法,然后在此viewController上添加scrollview。
第二,我建议:创建一个自定义类,该类是从UIScrollView继承的,如下所示:
。H:
@interface CustomScrollView : UIScrollView
@end
.m:
#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);
}
关于ios - 如何知道用户在屏幕上触摸的 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19513772/