将滑动手势从 UIView 转发到 UIScrollView
Forward swipe gesture from UIView to UIScrollView
我的应用程序中的主要 UIView
包含一个 UIScrollView
。我在主视图中添加了 UISwipeGestureRecognizer
:
@IBOutlet weak internal var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(sender:)))
view.addGestureRecognizer(swipeRecognizer)
}
我已经像这样删除了识别器方法...但我不知道如何将手势传递给滚动视图。
func handleSwipes(sender: UISwipeGestureRecognizer) {
if sender.direction == .left {
// Send left swipe to the UIScrollView
} else if (sender.direction == .right) {
// Send right swipe to the UIScrollView
}
}
您需要的是:
- 通过响应链传播触摸的直通视图
- 在
UIScrollView
上模拟滑动手势
透视视图
您可以子类化您的 scrollView
并覆盖 pointInside:event:
方法
// you should convert this into Swift :(
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *view in self.subviews) {
if (!view.hidden && view.userInteractionEnabled &&
[view pointInside:[self convertPoint:point toView:view] withEvent:event])
return YES;
}
return NO;
}
然后您的自定义 scrollView
会将手势传播到其下方的其他视图,您可以在 mainView
中收听滑动手势
在 UIScrollView
上模拟滑动手势
您可以通过 UIScrollView
的 setContentOffset
方法来做到这一点。如果您将内容偏移量设置在其范围之外,这可能会让人感觉有问题,请小心!
但是,恕我直言,最好的解决方案是更改视图层次结构并在滚动视图顶部的视图上收听滑动手势。
我的应用程序中的主要 UIView
包含一个 UIScrollView
。我在主视图中添加了 UISwipeGestureRecognizer
:
@IBOutlet weak internal var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(sender:)))
view.addGestureRecognizer(swipeRecognizer)
}
我已经像这样删除了识别器方法...但我不知道如何将手势传递给滚动视图。
func handleSwipes(sender: UISwipeGestureRecognizer) {
if sender.direction == .left {
// Send left swipe to the UIScrollView
} else if (sender.direction == .right) {
// Send right swipe to the UIScrollView
}
}
您需要的是:
- 通过响应链传播触摸的直通视图
- 在
UIScrollView
上模拟滑动手势
透视视图
您可以子类化您的 scrollView
并覆盖 pointInside:event:
方法
// you should convert this into Swift :(
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
for (UIView *view in self.subviews) {
if (!view.hidden && view.userInteractionEnabled &&
[view pointInside:[self convertPoint:point toView:view] withEvent:event])
return YES;
}
return NO;
}
然后您的自定义 scrollView
会将手势传播到其下方的其他视图,您可以在 mainView
在 UIScrollView
上模拟滑动手势
您可以通过 UIScrollView
的 setContentOffset
方法来做到这一点。如果您将内容偏移量设置在其范围之外,这可能会让人感觉有问题,请小心!
但是,恕我直言,最好的解决方案是更改视图层次结构并在滚动视图顶部的视图上收听滑动手势。