Iphone 如果子视图';s按钮按下
我正努力从手势识别器中获得我想要的行为,特别是在其他人开火时取消某些手势 我有一个scrollView设置为分页,每个页面中有多个子视图。我添加了一个触摸手势识别器,如果用户点击页面的右侧或左侧,可以滚动到下一页或上一页Iphone 如果子视图';s按钮按下,iphone,cocoa-touch,uibutton,uigesturerecognizer,Iphone,Cocoa Touch,Uibutton,Uigesturerecognizer,我正努力从手势识别器中获得我想要的行为,特别是在其他人开火时取消某些手势 我有一个scrollView设置为分页,每个页面中有多个子视图。我添加了一个触摸手势识别器,如果用户点击页面的右侧或左侧,可以滚动到下一页或上一页 // Add a gesture recogniser turn pages on a single tap at the edge of a page UITapGestureRecognizer *tapGesture = [[UITapGestureRec
// 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];
}
-(void)tapGestureHandler:(UIGestureRecognitor*)GestureRecognitor{
常量CGKTAPMARGIN=180;
//获取窗口坐标系中点击点的位置
CGPoint tapPoint=[手势识别器位置查看:无];
//如果点击点位于页面左侧,则返回页面
if(tapPoint.x>(self.frame.size.width-kTapMargin))[self-scrollRectToVisible:pageViewRightFrame动画:是];
//如果点击点位于页面右侧,则向前移动页面
else if(tapPoint.x
除了我在页面上有一个包含按钮的子视图之外,所有这些都可以正常工作。如果用户触摸子视图上的一个按钮,而我不知道如何做,我希望能够忽略点击来翻页
干杯
Dave您可以将UIView子类化并覆盖
-touchsbegind
选择器,或者您可以使用子视图的不透明
属性使其对触摸“不可见”(如果view.opaque=NO,视图将忽略触摸事件).最终对我来说效果最好的解决方案是使用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];
}
}
-(void)tapGestureHandler:(UIGestureRecognitor*)GestureRecognitor{
常量CGKTAPMARGIN=180;
//获取窗口坐标系中点击点的位置
CGPoint tapPoint=[手势识别器位置查看:无];
//如果此点击下方没有按钮,则如果靠近页面边缘,则移动到下一页
UIView*ViewatBottofHeirachy=[self.window hitTest:tapPoint with event:nil];
如果(![ViewatBottofHeirachy是类:[UIButton类]]){
//如果点击点位于页面左侧,则返回页面
if(tapPoint.x>(self.bounds.size.width-kTapMargin))[self-scrollRectToVisible:pageViewRightFrame动画:是];
//如果点击点位于页面右侧,则向前移动页面
else if(tapPoint.x
显示了答案:
- (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将是页面上包含按钮(甚至按钮)的子视图 设置UITapGestureRecognitor UITapGestureRecognitor委托方法:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return touch.view != buttonThatShouldCancelTapGesture
}
你好,鲁本,谢谢你的建议。我不太确定他们在这种情况下是否会有所帮助。基本上,我想在子视图中按住按钮,然后在超级视图中取消触摸。据我所知,UIGestureRecognizer将首先获得所有触摸,我不确定UIButton是否使用手势识别器或自制触摸来完成操作。好的,我通过在顶视图中添加长按手势识别器解决了类似情况:当用户长按页面时,它可以访问子视图(以及您想要的所有按钮)。使用UILongPressGestureRecognitizer干杯鲁本,将尝试一下,并让您知道我的进展情况。嗨,鲁本,很抱歉,我无法实现此功能,感觉我添加了越来越多的代码。我已经想出了一个解决方案(请参阅我的答案)在我得到关于其有效性的反馈之前,我不会接受这个。感谢所有的帮助,Dave。在我的最后一个回答中,我想提出一个可能不符合您要求的解决方案。我的观点是,您可以在上方视图中放置UILongPressGestureRecognitor,当检测到它时,将动画化该视图,使其消失并让其消失用户触摸下面的所有按钮。这不是您真正需要的!很抱歉,这是我能想到的解决问题的最好方法。太好了!这似乎是我想要的答案,但我在哪里实现这个tapGestureHandler?它是如何触发的?干杯。@f0rz我问题中的第一段代码设置了一个tap手势识别器在我自己的类中,它是UIView的一个子类。实际上,您创建了一个UIGestureRecognitzer对象,并使用UIView的AddGestureRecognitzer:方法添加它。@MagicBulletDave:我遇到了类似的问题,使用您的解决方案,如果层次结构底部有UIButton,我取消了手势,但我想知道如何添加我是否让我的UIButton获取它未获取的press事件。您好,您需要确保您的手势识别器的CancelsTouchinView属性设置为否。
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return touch.view != buttonThatShouldCancelTapGesture
}