将触摸转发到 UIPageViewController
Forward touches to UIPageViewController
我有一个包含 UIPageViewController
视图的容器视图。它位于 UIViewController
内,占据了整个屏幕。在容器视图的顶部,我有一个 UIView
,覆盖了一半的屏幕,其中包含一个按钮和一些文本。我想将此 UIView
的触摸转发到 UIPageViewController
。这样一来,即使用户在 UIView
上滑动,UIPageViewController
仍然可以滑动 left/right。我还希望能够按下按钮,因此不能只在 UIView
.
上将 isUserInteractionEnabled 设置为 false
我该怎么做?
hitTest 是决定谁应该消费 touches/gestures 的方法。
所以你的“UIView
,覆盖了一半的屏幕”可以从 NoTouchHandlerView
类中继承。然后这个view就不会消耗touches了。然后它将传递给它下面的视图。
class NoTouchHandlerView: UIView
{
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
{
if let hitTestView = super.hitTest(point, with: event), hitTestView !== self {
return hitTestView
}else {
return nil
}
}
}
Objective C 为像我这样的懒人所接受的答案版本:)
@implementation NoTouchHandlerView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView* hitTestView = [super hitTest:point withEvent:event];
if (hitTestView != nil && hitTestView != self) {
return hitTestView;
}
return nil;
}
@end
我有一个包含 UIPageViewController
视图的容器视图。它位于 UIViewController
内,占据了整个屏幕。在容器视图的顶部,我有一个 UIView
,覆盖了一半的屏幕,其中包含一个按钮和一些文本。我想将此 UIView
的触摸转发到 UIPageViewController
。这样一来,即使用户在 UIView
上滑动,UIPageViewController
仍然可以滑动 left/right。我还希望能够按下按钮,因此不能只在 UIView
.
我该怎么做?
hitTest 是决定谁应该消费 touches/gestures 的方法。
所以你的“UIView
,覆盖了一半的屏幕”可以从 NoTouchHandlerView
类中继承。然后这个view就不会消耗touches了。然后它将传递给它下面的视图。
class NoTouchHandlerView: UIView
{
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
{
if let hitTestView = super.hitTest(point, with: event), hitTestView !== self {
return hitTestView
}else {
return nil
}
}
}
Objective C 为像我这样的懒人所接受的答案版本:)
@implementation NoTouchHandlerView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView* hitTestView = [super hitTest:point withEvent:event];
if (hitTestView != nil && hitTestView != self) {
return hitTestView;
}
return nil;
}
@end