我一直在试图弄清楚如何将UIScrollView的触摸转发到其superview。原因是要按照触摸的方式对光标进行排序。无论如何,滚动视图填充了UIButtons。我转发触摸的代码是在scrollview子类中使用委托:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:@selector(tfDelegateBegan:)]) {
[[self tfDelegate]tfDelegateBegan:[touches anyObject]];
}
NSLog(@"touches began");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:@selector(tfDelegateMoved:)]) {
[[self tfDelegate]tfDelegateMoved:[touches anyObject]];
}
NSLog(@"touches moved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:@selector(tfDelegateEnded:)]) {
[[self tfDelegate]tfDelegateEnded:[touches anyObject]];
}
NSLog(@"touches ended");
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesCancelled:touches withEvent:event];
if ([[self tfDelegate]respondsToSelector:@selector(tfDelegateCancelled:)]) {
[[self tfDelegate]tfDelegateCancelled:[touches anyObject]];
}
NSLog(@"touches cancelled");
}
但是,我了解到UIScrollviews是通过UIGestureRecognizers进行操作的,因此默认情况下甚至不会调用这些方法。我意识到手势识别器在iOS 5中暴露,但我也需要支持4.0。我这样做了:
NSArray *a = [[theScrollview gestureRecognizers]retain];
for (UIGestureRecognizer *rec in a) {
if ([rec isKindOfClass:[UIPanGestureRecognizer class]]) {
NSLog(@"this is the pan gesture");
rec.cancelsTouchesInView = NO;
}
}
这允许手势工作和触摸方法同时被调用。问题是,现在如果您尝试在触摸按钮时滚动,则可以在滚动时按下按钮。通常,滚动取消按钮,并且只有滚动视图不滚动时才能按下按钮。 这是所需的功能。关于如何实现这一目标的任何建议?
答案 0 :(得分:1)
也许尝试使用一个标志来控制按钮操作,如果滚动视图正在滚动,则会阻止事件触发。
BOOL isScrolling = NO;
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
isScrolling = YES;
}
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
isScrolling = NO;
}
- (void) didTapButton {
if(isScrolling)
return;
//Do Button Stuff
}