[英]How to recognize swipe gesture in UIScrollView
我正在尝试在UIScrollView
识别左/右滑动手势。 我试图创建UISwipeGestureRecognizers
并将它们与滚动视图相关联。 它的工作原理很少见。 大部分时间我都没有被召唤。 为什么?
如何可靠地左/右滑动工作? 我可以使用手势识别器,还是我必须以touchesBegan/Ended
自己处理它
谢谢
弄清楚了。 在我的例子中,我的UIScrollView包含一个允许缩放的UIImage。 显然这意味着滚动已启用且UIScrollView难以区分用于滚动和滑动的手势(下一个,上一个图像)。
在我的情况下,关键是在图像未放大时禁用滚动视图中的滚动,并在放大图像时重新设置它。这提供了预期的行为。
关键部分是将以下内容放在滚动视图的委托中:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if (scrollView.zoomScale!=1.0) {
// Zooming, enable scrolling
scrollView.scrollEnabled = TRUE;
} else {
// Not zoomed, disable scrolling so gestures get used instead
scrollView.scrollEnabled = FALSE;
}
}
我还必须在禁用滚动的情况下初始化滚动视图。 要启用缩放,只需在委托调用上提供图像,
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// Return the scroll view
return myImage;
}
并在viewDidLoad中设置一些参数用于缩放和设置手势识别器
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1.0;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
[myScrollView addSubview:myImage];
[self setWantsFullScreenLayout:TRUE];
myScrollView.scrollEnabled = FALSE;
UISwipeGestureRecognizer *recognizer =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
recognizer.delaysTouchesBegan = TRUE;
[myScrollView addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[myScrollView addGestureRecognizer:recognizer];
[recognizer release];
[myScrollView delaysContentTouches];
}
UIScrollView *scrollView = ...
UISwipeGestureRecognizer *mySwipe = ...
解决此问题的正确解决方案是添加一行代码:
[scrollView.panGestureRecognizer requireGestureRecognizerToFail:mySwipe]
Swift版本:
scrollView.panGestureRecognizer.requireGestureRecognizerToFail(mySwipe)
Swift4版本:
scrollView.panGestureRecognizer.require(toFail: mySwipe!);
好帖子。
我正在做类似的事情(没有图像视图),如果contentSize小于高度(我的滚动视图只滚动垂直),我基本上必须禁用滚动。
if (scrollView.contentSize.height>scrollView.frame.size.height) {
scrollView.scrollEnabled = YES;
}
else {
scrollView.scrollEnabled = NO;
}
这对我有用
对于那些想要动画和自定义滑动手势识别器的人。
我们可以使用UIScrollView和UIGestureRecognizer委托:
Class ViewController: UIViewController, UISCrollViewDelegate, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
swipeLeft.delegate = self
swipeRight.delegate = self
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return scrollView.alwaysBounceHorizontal
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Your custom animation at the end of scrolling.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.