UIPageViewController手势识别器 [英] UIPageViewController Gesture recognizers
问题描述
我已经在申请了一段时间,但由于NDA,我无法提出这个问题。
I've been working on an application for a while now, but I couldn't ask this question due to the NDA.
我的ViewController加载了UIPageViewController。视图控制器具有由PageViewControllers手势识别器覆盖的按钮。例如,我在viewcontroller的右侧有一个按钮,当你按下按钮时,PageViewController接管并更改页面。
I have a UIPageViewController load with my Viewcontroller. The view controllers have buttons which are overridden by the PageViewControllers gesture recognizers. For example I have a button on the right side of the viewcontroller and when you press the button, the PageViewController takes over and changes the page.
如何制作按钮接收触摸并取消PageViewController中的手势识别器?
How can I make the button receive the touch and cancel the gesture recognizer in the PageViewController?
我认为PageViewController使我的ViewController成为其视图的子视图。
I think the PageViewController makes my ViewController a subview of its view.
我知道我可以关闭所有的手势,但这不是我想要的效果。
I know I could turn off all of the Gestures, but this isn't the effect I'm looking for.
我会更喜欢不要将PageViewController子类化,因为apple说这个类不是要子类化的。
I would prefer not to subclass the PageViewController as apple says this class is not meant to be subclassed.
推荐答案
你可以覆盖
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
更好地控制PageViewController何时应该接收触摸而不是。请参阅 Dev API手势识别器
to better control when the PageViewController should receive the touch and not. Look at "Preventing Gesture Recognizers from Analyzing Touches" in Dev API Gesture Recognizers
我的解决方案如下所示UIPageViewController的RootViewController:
My solution looks like this in the RootViewController for the UIPageViewController:
在viewDidLoad中:
In viewDidLoad:
//EDITED Need to take care of all gestureRecogizers. Got a bug when only setting the delegate for Tap
for (UIGestureRecognizer *gR in self.view.gestureRecognizers) {
gR.delegate = self;
}
覆盖:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//Touch gestures below top bar should not make the page turn.
//EDITED Check for only Tap here instead.
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
CGPoint touchPoint = [touch locationInView:self.view];
if (touchPoint.y > 40) {
return NO;
}
else if (touchPoint.x > 50 && touchPoint.x < 430) {//Let the buttons in the middle of the top bar receive the touch
return NO;
}
}
return YES;
}
并且不要忘记将RootViewController设置为UIGestureRecognizerDelegate。
And don't forget to set the RootViewController as UIGestureRecognizerDelegate.
(仅供参考,我只是在横向模式下。)
(FYI, I'm only in Landscape mode.)
编辑 - 上面的代码翻译成Swift 2:
EDIT - The above code translated into Swift 2:
在viewDidLoad中:
In viewDidLoad:
for gr in self.view.gestureRecognizers! {
gr.delegate = self
}
使页面视图控制器继承 UIGestureRecognizerDelegate
然后添加:
Make the page view controller inherit UIGestureRecognizerDelegate
then add:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if let _ = gestureRecognizer as? UITapGestureRecognizer {
let touchPoint = touch .locationInView(self.view)
if (touchPoint.y > 40 ){
return false
}else{
return true
}
}
return true
}
这篇关于UIPageViewController手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!