防止 UIScrollView 将内容移动到左上角
我有一个 UIImageView
包含在 UIScrollView
中。图像(通常)很大,因此用户可以将其缩小以查看整个图像。
不过,缩小后,UIScrollView
会将 ImageView
捕捉到滚动视图的左上角。我希望用户可以定位它,但还没有找到“关闭它”的方法。
这有点像始终允许滚动,而不是仅在图像放大时才允许滚动。也许这是一个太大的变化?
有谁知道有什么办法吗?最初,我只是想手动创建这个功能。但是 UIImageViews 不喜欢调整到新的尺寸(我已经尝试了所有方法,但无法让 UIImageView 调整大小,除非我从 imageView 中删除图片,更改框架,然后重新添加)。
I have a UIImageView
contained within a UIScrollView
. The image is (usually) big, so the user can zoom it out in order to see the whole thing.
Upon zooming out, though, UIScrollView
snaps the ImageView
to the top-left of the scrollview. I want this to be positionable by the user, and haven't found a way to "turn it off" yet.
It's kinda like always allowing scrolling, rather then only allowing scrolling when the image is zoomed in. Maybe it's too major of a change?
Anyone know of a way? Originally, I was just going to create this functionality manually. But UIImageViews don't like to adjust to new sizes (I've tried about everything and can't get UIImageView
to resize UNLESS I remove the picture from the imageView
, change the frame, and re-add it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终禁用了
UIScrollView
的panGestureRecognizer
并在自定义中进行了替换。这里有一个关于如何禁用它的快速片段:
有点像黑客工作,但这是由于
UIScrollViews
已将 GestureRecognizer 的类更改为“UIScrollViewPanGestureRecognizer”。如果您尝试使用该类,编译器会对您大喊大叫(可能有更好的解决方案)。I ended up disabling the
UIScrollView
'spanGestureRecognizer
and subbing in a custom one.Here's a quick snippet on how to disable it:
A bit of a hack-job there, but it's due to the fact that
UIScrollViews
have changed the class of the GestureRecognizer to "UIScrollViewPanGestureRecognizer." The compiler will yell at you if you try to use that class (there's probably a better solution out there).如果您将
UIImageView
定位在UIScrollView
中的 0,0 位置,那么它始终位于左上角。如果您希望它在滚动视图小于视图时居中,则需要将其放置在那里。检查其-size
是否大于或小于滚动视图的大小。如果它比滚动视图小,请将其-center
设置为与滚动视图相同。If you locate the
UIImageView
at location 0,0 in theUIScrollView
, then that's always going to be the upper-left. If you want it to be centered in the scrollview when it's smaller than the view, you need to position it there. Check whether its-size
is bigger or smaller than the scrollview's. If it's smaller than the scrollview, set its-center
to be the same as the scrollview's.