简体   繁体   English

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

[英]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. 我有UIScrollView并且滚动视图框架不等于UIView框架。 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 . 我实际上刚刚学习了一个更简单的解决方案:只需将view.addGestureRecognizer(scrollView.panGestureRecognizer)添加到viewDidAppearviewDidLoad

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. 这本质上确实是采取scrollViewUIPanGestureRecognizer ,手势识别器识别手指拖动并滚动scrollView的内容,并将其提供给基视图,以便它可以识别手指拖动并滚动scrollView过的内容。

So you don't need all the math and scrollRectToVisible as described below. 因此,您不需要如下所述的所有math和scrollRectToVisible Oh well, I think it's useful and fun to learn more about how UIScrollViews work! 哦,对了,我认为学习UIScrollViews的工作原理很有用又有趣!

OLD

To programmatically make the UIScrollView "scroll", use UIScrollView's scrollRectToVisible . 要以编程方式使UIScrollView“滚动”,请使用UIScrollView的scrollRectToVisible

See Programmatically scroll a UIScrollView for more details and code syntax. 有关更多详细信息和代码语法,请参见以编程方式滚动UIScrollView

Basically, at any point in time, a UIScrollView's content subview is offset some amount from its frame. 基本上,在任何时间点,UIScrollView的内容子视图都会从其框架中偏移一些量。

It helps to understand the parts to a UIScrollView (esp. the content subview), like in this picture (feel free to ignore the purple UIImageView): 它有助于理解UIScrollView的各个部分(尤其是内容子视图),如下图所示(可以随意忽略紫色的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). 在第一个图像中,UIScrollView的内容子视图(红色轮廓)根本没有偏离UIScrollView的框架(假定与可见屏幕的大小相同):两者在(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. 在第二个/中间图像中,UIScrollView的内容子视图现在偏移了“上”:该内容子视图的左上角现在位于类似(0,-200)的位置,其中(0,0)仍然是该对象的左上角UIScrollView的框架。

Again, you can change the UIScrollView's content subview offset, and thus simulate a "scroll", using scrollRectToVisible , and the parameter animated: true . 再次,您可以更改UIScrollView的内容子视图偏移量,并使用scrollRectToVisible以及参数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. 提示 :例如, scrollView.contentOffset.x内容向左滚动(在用户从右向左滑动之后),您将采用UIScrollView的当前内容偏移量的x坐标( scrollView.contentOffset.x ),然后减去 UIScrollView的宽度( scrollView.frame.size.width )。 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. 因此,如果UIScrollView的当前内容偏移量(左上角)为(0,0),则内容偏移量(左上角)将从(0-width,0)开始移动,内容“向左”移动。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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