且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

识别UIView中的滑动手势以使用手势识别器滚动scrollView

更新时间:2022-10-18 08:13:47

编辑



我实际上刚刚学到了一个更简单的解决方案:只需将 view.addGestureRecognizer(scrollView.panGestureRecognizer)添加到您的 viewDidAppear viewDidLoad



这实际上是基于 scrollView



在第一张图片中,UIScrollView的内容子视图(红色轮廓)没有偏离UIScrollView的框架(假设大小相同)作为可见屏幕):两者在(0,0)处都有相同的左上角。



在第二个/中间图像中,UIScrollView的内容子视图现在是offsetup:内容子视图的左上角现在是(0,-200),其中(0,0)仍然是UIScrollView框架的左上角。



同样,您可以更改UIScrollView的内容子视图偏移量,从而使用 scrollRectToVisible 模拟滚动,以及参数 anim ated:true



提示:让内容向左滚动(例如,在用户从右向左滑动之后),你将采用UIScrollView的当前内容偏移量的x坐标( scrollView.contentOffset.x )和减去 UIScrollView的宽度( scrollView.frame。 size.width )。因此,如果UIScrollView的当前内容偏移量(左上角)为(0,0),则内容偏移量(左上角)从(0-宽度,0)移动,内容向左移动。


I have UIScrollView and the scroll view frame is not equal to the UIView frame. So, scroll view contents are not swiped while swiping outside the scroll view frame. But I want the whole screen responsive for the scroll view though the scroll view doesn't cover full screen. I know that can be done by making the scroll view frame equal to the view frame.But I don't want to do it. I tried other possible solutions I found so far. But nothing is working as I want. I need to do this using gesture recognizer.

Following is my code:

override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.delegate = self

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
        swipeRight.direction = UISwipeGestureRecognizerDirection.right
        self.view.addGestureRecognizer(swipeRight)


        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
        swipeLeft.direction = UISwipeGestureRecognizerDirection.left
        self.view.addGestureRecognizer(swipeLeft)

}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
        if let swipeGesture = gesture as? UISwipeGestureRecognizer {
            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.right:

                scrollView.panGestureRecognizer.isEnabled = true

            case UISwipeGestureRecognizerDirection.left:

                 scrollView.panGestureRecognizer.isEnabled = true


            default:
                break
            }
        }
    }

This is my view controller and the blue background is the scroll view. Swipe gesture works only on that blue region. But I want it to work through the screen even in corners. How can I achieve it using gesture recognizer?

EDIT

I actually just learned about a much simpler solution: just add view.addGestureRecognizer(scrollView.panGestureRecognizer) to your viewDidAppear or viewDidLoad.

What this essentially does is take the scrollView's UIPanGestureRecognizer, a gesture recognizer that recognizes finger dragging and scrolls the scrollView's content, and gives it to the base view so it can recognize finger dragging and scroll the scrollView's content too.

So you don't need all the math and scrollRectToVisible as described below. Oh well, I think it's useful and fun to learn more about how UIScrollViews work!

OLD

To programmatically make the UIScrollView "scroll", use UIScrollView's scrollRectToVisible.

See Programmatically scroll a UIScrollView for more details and code syntax.

Basically, at any point in time, a UIScrollView's content subview is offset some amount from its frame.

It helps to understand the parts to a UIScrollView (esp. the content subview), like in this picture (feel free to ignore the purple UIImageView):

In the first image, the UIScrollView's content subview (red outline) is not offset from the UIScrollView's frame (assumed to be the same size as the visible screen) at all: both have the same top left corner at (0, 0).

In the second/middle image, the UIScrollView's content subview is now offset "up": the content subview's top left corner is at something like (0, -200) now, where (0, 0) is still the top left corner of the UIScrollView's frame.

Again, you can change the UIScrollView's content subview offset, and thus simulate a "scroll", using scrollRectToVisible, and the parameter animated: true.

Hint: to have the content scroll left (after the user swipes right-to-left) for example, you'd take the UIScrollView's current content offset's x coordinate (scrollView.contentOffset.x) and subtract the width of the UIScrollView (scrollView.frame.size.width) to it. So if the UIScrollView's current content offset (top-left corner) is (0, 0), the content offset (top-left corner) moves from (0-width, 0), and the content "moves" left.