【发布时间】:2013-12-03 04:12:19
【问题描述】:
我有一个实现touchesBegan, touchesMoved, touchesEnded 的子类UIView。一切运作良好。然后,我在这个UIVIew 中添加了一个UIScrollview 作为子视图。在UIScrollview 中进行的触摸是not forwarded back up the responder chain to the parent view,因此我的 UIView 的触摸方法永远不会被调用(它们被调用用于在滚动视图之外进行的触摸就好了)。
我像这样创建了一个新的 UIScrollView 类别:
@implementation UIScrollView (forwardTouches)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesBegan: touches withEvent:event];
[self setScrollEnabled:NO];
NSLog(@"touchesBegan");
}
else{
NSLog(@"touchesBegan-else");
[super touchesEnded: touches withEvent: event];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesMoved: touches withEvent:event];
NSLog(@"touchesMoved");
}
else{
NSLog(@"touchesMoved - else");
[super touchesEnded: touches withEvent: event];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesEnded: touches withEvent:event];
[self setScrollEnabled:YES];
NSLog(@"touchesEnded");
}
else{
NSLog(@"touchesEnded - else");
[super touchesEnded: touches withEvent: event];
}
}
这实际上似乎在 iPhone 上按预期工作(即:如果用户将手指拖过滚动视图,它会滚动,但如果他将手指放在视图上并且不移动它,则父视图的 touchesBegan 方法火灾),但它不适用于 iPad。据推测,self.dragging 需要更多时间在 iPad 上,但我正在努力寻找可靠解决此问题的方法。我确定我遗漏了一些非常明显的东西。提前致谢!
使用可能的解决方案进行编辑:
经过反复试验,我想出了下面的代码。它似乎在 iPhone 和 iPad 上运行良好。为什么使用它可能是个坏主意?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray * myDataArray = [NSArray arrayWithObjects:touches, event, nil];
// If not dragging, send event to next responder
if (!self.dragging){
[self performSelector:@selector(forwardTouches:) withObject:myDataArray afterDelay:0.5];
}
else{
NSLog(@"touchesBegan-else");
[super touchesEnded: touches withEvent: event];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
}
-(void)forwardTouches:(NSArray *)array{
NSSet * mySet = [array objectAtIndex:0];
UIEvent * myEvent = [array objectAtIndex:1];
if (!self.dragging){
[self.nextResponder touchesBegan:mySet withEvent: myEvent];
[self setScrollEnabled:NO];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesMoved: touches withEvent:event];
NSLog(@"touchesMoved");
}
else{
NSLog(@"touchesMoved - else");
[super touchesEnded: touches withEvent: event];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// If not dragging, send event to next responder
if (!self.dragging){
[self.nextResponder touchesEnded: touches withEvent:event];
[self setScrollEnabled:YES];
NSLog(@"touchesEnded");
}
else{
NSLog(@"touchesEnded - else");
[super touchesEnded: touches withEvent: event];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
}
【问题讨论】:
-
您是否只是想在父视图中捕获点击事件而不中断滚动视图中的滚动?
-
我试图得到一个非此即彼的场景。如果用户将手指按在屏幕上并且不移动它,我希望 UIView 中的触摸方法触发,并且滚动视图不滚动。如果用户在屏幕上按下手指并移动它,我希望 UIScrollview 滚动,并且 UIView 的触摸方法不触发。
标签: ios iphone objective-c uiview uiscrollview