我正在努力从手势识别器中获得我想要的行为,特别是在其他手势被解雇的情况下取消某些手势。
我有一个设置为分页的scrollView和每个页面中的多个子视图。我已经添加了一个触摸手势识别器,如果用户点击页面的右侧或左侧,可以滚动到下一页或上一页。
// Add a gesture recogniser turn pages on a single tap at the edge of a page
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
tapGesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:tapGesture];
[tapGesture release];
和我的手势处理器:
- (void) tapGestureHandler:(UIGestureRecognizer *) gestureRecognizer {
const CGFloat kTapMargin = 180;
// Get the position of the point tapped in the window co-ordinate system
CGPoint tapPoint = [gestureRecognizer locationInView:nil];
// If the tap point is to the left of the page then go back a page
if (tapPoint.x > (self.frame.size.width - kTapMargin)) [self scrollRectToVisible:pageViewRightFrame animated:YES];
// If the tap point is to the right of the page then go forward a page
else if (tapPoint.x < kTapMargin) [self scrollRectToVisible:pageViewLeftFrame animated:YES];
}
所有的工作都很好,除了我在页面上有一个子视图里面有按钮的地方。如果用户触摸subView上的按钮,而我不知道该如何操作,我希望能够忽略轻触翻页的操作。
干杯
戴夫
发布于 2011-05-09 18:28:36
最终对我来说最有效的解决方案是使用hitTest来确定在点击手势的位置下是否有任何按钮。如果有,那就忽略手势代码的其余部分。
看起来效果很好。我想知道我的所作所为是否有什么问题。
- (void) tapGestureHandler:(UIGestureRecognizer *) gestureRecognizer {
const CGFloat kTapMargin = 180;
// Get the position of the point tapped in the window co-ordinate system
CGPoint tapPoint = [gestureRecognizer locationInView:nil];
// If there are no buttons beneath this tap then move to the next page if near the page edge
UIView *viewAtBottomOfHeirachy = [self.window hitTest:tapPoint withEvent:nil];
if (![viewAtBottomOfHeirachy isKindOfClass:[UIButton class]]) {
// If the tap point is to the left of the page then go back a page
if (tapPoint.x > (self.bounds.size.width - kTapMargin)) [self scrollRectToVisible:pageViewRightFrame animated:YES];
// If the tap point is to the right of the page then go forward a page
else if (tapPoint.x < kTapMargin) [self scrollRectToVisible:pageViewLeftFrame animated:YES];
}
}
发布于 2013-08-02 00:15:45
Apple documentation显示了答案:
- (void)viewDidLoad {
[super viewDidLoad];
// Add the delegate to the tap gesture recognizer
self.tapGestureRecognizer.delegate = self;
}
// Implement the UIGestureRecognizerDelegate method
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch: (UITouch *)touch {
// Determine if the touch is inside the custom subview
if ([touch view] == self.customSubview){
// If it is, prevent all of the delegate's gesture recognizers
// from receiving the touch
return NO;
}
return YES;
}
当然,在本例中,customSubview应该是包含按钮(甚至是按钮)的页面上的子视图。
发布于 2011-05-04 01:08:37
您可以子类化UIView并覆盖-touchesBegan
选择器,或者您可以使用子视图的opaque
属性使它们对触摸“不可见”(如果view.opaque = NO,视图将忽略触摸事件)。
https://stackoverflow.com/questions/5866520
复制相似问题