UIScrollView image/photo viewer with paging enabled and zooming

后端 未结 8 900
有刺的猬
有刺的猬 2020-12-07 07:01

OK, I think it\'s time to make an official place on the internet for this problem: How to make a UIScrollView photoviewer with paging and zooming. Welcome my fe

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 07:46

    I did some playing around with the native Photos app, and I think I can say with confidence they are using a single UIScrollView. The giveaway is this: zoom in on an image, and pull to the left or right. You will see the next or previous photo. If you tug hard enough, it will even page to the next photo at 1.0f zoom. Flip back and the previously zoomed photo will be back to 1.0f zoom as well.

    Obivously I didn't write Photos.app, but I'll take a wild guess at how they did it:

    • A single UIScrollView and a single UIScrollViewDelegate
    • Populate the UIScrollView with UIImageView children
    • Listen for scrollViewDidScroll:
    • Do some math and figure out what page you are currently on
    • Listen for viewForZoomingInScrollView:
    • Return a different view depending on the page index
    • Listen for scrollViewDidEndZooming:withView:atScale: and optionally do some anti-aliasing, etc based on the content

    If you decide to try that out, let me know how it works out for you. I'd love to know how you finally end up getting this to work. Even better, post it to github.

提交回复
热议问题