Using UIScrollView, when Zooming Out, how can I keep the content view at the center?

北城以北 提交于 2019-12-21 20:56:11

问题


The minimum scale is 0.5

The maximum scale is 2.0

I need to always use larger contentSize for UIScrollView to be able to drag around the content. So, as the CGRect of the content is changing, contentSize of UIScrollView is also changing.

However, as I zoom out, if the scale is going below 1.0, contentOffset is abruptly changed to {0.0, 0.0}, making it very difficult to resize the content while keep it's position to the overlay view (e.g. a clothe image).

In other words, I can't make the content view smaller while keeping it positioned at the center of the UIScrollView.

Please help me to find the right way to use zoom in/out with UIScrollView while keeping the position of the content view.


回答1:


The solutions is basically about using contentInset at initial setup to keep the content at the center, instead of contentOffset. With contentInset being unmodified, zoomed in or out object can be at the center all the time.




回答2:


This has been answered pretty well in the post: UIScrollView with centered UIImageView, like Photos app

In particular, refer to JosephH's answer which itself is culled from the 2010 WWDC presentation.

This is also covered in Matt Neuburg's book, using the same WWDC reference: http://www.amazon.com/Programming-iOS-6-Matt-Neuburg/dp/1449365760/ref=sr_1_1?ie=UTF8&qid=1367363707&sr=8-1&keywords=programming+ios6

The source of which is available here (see chapter 20): https://github.com/mattneub/Programming-iOS-Book-Examples

The open question for me, is can this not be done as part of the constraints.



来源:https://stackoverflow.com/questions/9197572/using-uiscrollview-when-zooming-out-how-can-i-keep-the-content-view-at-the-cen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!