gpt4 book ai didi

iphone - 如果按下 subview 的按钮如何取消 UIGestureRecognizer

转载 作者:行者123 更新时间:2023-12-03 18:20:43 24 4
gpt4 key购买 nike

我正在努力从手势识别器中获得我想要的行为,特别是在其他手势已触发时取消某些手势。

我有一个 ScrollView 设置为分页和每个页面中的多个 subview 。我添加了一个触摸手势识别器,如果用户点击页面的右侧或左侧,则可以滚动到下一页或上一页。

    // 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 。如果用户触摸 subview 上的按钮,我希望能够忽略翻页的点击,但我不知道如何执行此操作。

干杯

戴夫

最佳答案

最终对我来说最有效的解决方案是使用 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];
}
}

关于iphone - 如果按下 subview 的按钮如何取消 UIGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5866520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com