ScrollView - 手势识别器 - 垂直滑动 [英] ScrollView - Gesture Recognizer - Swipe vertically

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

问题描述

我有一个UIScrollView在向左或向右滑动时有效,但是我减小了scrollView的大小,所以现在显示区域没有完全占据superview的框架,并且滑动仅在滚动视图的框架内工作。

I have a UIScrollView that works when swiping left or right, however I've reduced the size of the scrollView so, now display area doesn't fully occupy the superview's frame, and swiping works only within the frame of the scroll view.

我希望能够在缩小滚动视图的水平边界之外向上和向下滑动时垂直滚动。

I would like to be able to scroll vertically even when swiping up and down outside the horizontal bounds of the narrowed scroll view.

建议我使用手势识别器,但这超出了我目前对iOS的熟悉程度,可以使用更具体的建议或更多指导来开始使用它。

It was recommended that I use a gesture recognizer, but that's beyond my current familiarity with iOS and could use more specific advice or a bit more guidance to get started with that.

推荐答案

有一种更简单的方法然后使用手势识别器=]

There is a simpler approach then use a Gesture Recognizer =]

你可以设置的超级视图滚动视图(这是BIGGER ...)将触摸传递给滚动视图。它正在工作M-A-G-I-C-A-L-Y =]

You can setup the superview of the scroll view (which is BIGGER...) to pass the touches to the scroll view. It's working M-A-G-I-C-A-L-Y =]

首先,选择将所有触摸传递到滚动视图的视图。如果您的父视图已经可以使用它,您可以使用它。否则你应该考虑添加一个你希望接触的大小的新视图。

First, select the view that will pass all it's touches to the scroll view. if your parent view is already ok with that you may use it. otherwise you should consider add a new view in the size that you want that will catch touches.

现在创建一个新类(我将使用swift作为例子)

Now create a new class (I'll use swift for the example)

class TestView: UIView {

    @IBOutlet weak var Scroller: UIScrollView!

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

        let view = super.hitTest(point, with: event)

        if (view == self) {
            return Scroller
        }

        return view

    }

}

很好!现在您可以看到我们添加了卷轴的插座。所以使用界面构建器,选择新视图并将其类设置为身份检查器中的 TestView (或者您要使用的名称)用于您的自定义类。)

Nice! now as you can see we added an outlet of the scroller. so use interface builder, select the new view and set it's class to "TestView" in the identity inspector (Or to the name that you'll use for your custom class).

设置完课程后仍然选择了您的视图,请转到连接检查器并连接 Scroller 到故事板上的滚动视图。所有连接正确=]

After you set the class and your view is still selected go to connections inspector and connect "Scroller" to your scroll view on the storyboard. All connected properly =]

就是这样!没有手势识别器!
新视图会将它的所有内容传递给滚动视图,它的行为就像你在其中泛起一样=]

That's it!! no gesture recognizer needed!! The new view will pass all it's touches to the scroll view and it'll behave just like you pan in it =]

在我的回答中我使用了< a href =https://stackoverflow.com/a/7719901/2830676>回答

In my answer I used that answer

编辑:我现在改进了代码,它之前没有像预期的那样工作,现在它仅在需要时捕获,而不是像以前那样在应用程序中的每次触摸

I improved the code now, it wasn't working as expected before, now it catches only when in needs and not every touch in the app as before

这篇关于ScrollView - 手势识别器 - 垂直滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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