简体   繁体   中英

Recognize swipe gesture in UIView to scroll the scrollView using gesture-recognizer

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.

将滚动视图移到视图的框架,然后设置内容的插入/偏移,这样它只会一直显示到蓝色框架

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM