识别UIView中的滑动手势以使用手势识别器滚动scrollView [英] Recognize swipe gesture in UIView to scroll the scrollView using gesture-recognizer

查看:482
本文介绍了识别UIView中的滑动手势以使用手势识别器滚动scrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UIScrollView,滚动视图框架不等于UIView框架。因此,在滚动视图框外滑动时不会刷新滚动视图内容。但我希望整个屏幕响应滚动视图虽然滚动视图不覆盖全屏幕。
我知道可以通过使滚动视图框架等于视图框架来完成。但我不想这样做。到目前为止,我尝试了其他可能的解决方案。但没有什么能按我的意愿行事。 我需要使用手势识别器执行此操作。



以下是我的代码:

 覆盖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(手势:UIGestureRecognizer){
如果让swipeGesture =手势为? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:

scrollView.panGestureRecognizer.isEnabled = true

case UISwipeGestureRecognizerDirection.left:

scrollView.panGestureRecognizer.isEnabled = true


默认值:
break
}
}
}



这是我的视图控制器,蓝色背景是滚动视图。滑动手势仅适用于该蓝色区域。但我希望它甚至可以在角落里通过屏幕工作。 如何使用手势识别器来实现?

解决方案

编辑



我实际上刚刚学到了一个更简单的解决方案:只需将 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.

这篇关于识别UIView中的滑动手势以使用手势识别器滚动scrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆