NYOBetterZoom (NYOBetterZoomUIScrollView example project)
=========================================================
Summary
-------
This little example project stemmed from the fact that by default UIScrollView chucks small images to the top left corner of it's frame. I (and a lot of other people) would prefer it centred in the scrollview (like Photos.app). Having spent far too long trying to find a solution without any strange issues I finally got it to behave. To save other people some time going round the houses (like I initially did) I'm uploading this example solution.
Feel free to fork and use/improve this (and push back improvements!). It's a start but the code could certainly be improved upon.
How it works
------------
The solution I finally settled on is quite simple really (getting to this solution wasn't though...). I subclass UIScrollView and override it's setContentOffset: so that it returns a CGPoint that will result in the image being centred in it's UIScrollView superview if it's smaller than the UIScrollView.
How to use it
-------------
1. Include NYOBetterZoomUIScrollView in your project
2. \#import the .h and switch you [[UIScrollView alloc] init...] out for [[NYOBetterZoomUIScrollView alloc] init...]
3. Once the ScrollView is setup add your subview with setChildView: rather than addSubview:. (Alternatively you have an initWithChildView: and initWithFrame:andChildView: initialisers)
4. Have your view controller implement UIScrollViewDelegate and have viewForZoomingInScrollView: return the childView.
Possible step 5 - call [yourScrollView setContentOffset:CGPointZero] if your small view is initially appearing in the top left of the NYOBetterZoomUIScrollView instead of centred. With the v2.0 code changes though you hopefully won't need to do this any more.
Example project
---------------
The included example project has all the setup required to get a functioning ScrollView that centres small images (and large images when they're zoomed out).
It also shows how to be a bit smarter in regard to device rotation too (recalculating the view's aspect ratio & new minimum zoom when it's rotated).
The project also includes a variety of test images to ensure it works correctly with images of different dimensions (you can switch which image is used in the top of NYOBetterZoomViewController.m's viewDidLoad()):
* small.png (100x100) - Always smaller than the ScrollView frame, even when zoomed in 2x (maximumZoom).
* big.png (1000x1000)
* fit-portrait.png (320x460) - Same size as the portrait ScrollView's frame
* fit-landscape.png (480x300) - Same size as the landscape ScrollView's frame
* tall.png (500x1000)
* wide.png (1000x500)
类似QQ空间看图--大小图片放大缩小居中无偏移例子
4星 · 超过85%的资源 需积分: 13 197 浏览量
2012-03-27
16:15:59
上传
评论 7
收藏 52KB ZIP 举报
heyuan110
- 粉丝: 4
- 资源: 25
最新资源
- Qt开发知识、经验总结 包括Qss,数据库,Excel,Model/View等
- IV数据.xlsx
- 一些深度学习中的小例子,适合新手学习使用
- foldcraftlauncher_262944.apk
- 珍藏多年的基于matlab实现潮流计算程序源代码集合,包含多个潮流计算程序.rar
- 使用FPGA实现串-并型乘法器
- 基于matlab实现针对基于双曲线定位的DV-Hop算法中误差误差出一种基于加权双曲线定位的DV-Hop改进算法.rar
- 基于matlab实现由遗传算法开发的整数规划,车辆调度问题.rar
- 电视家7.0(对电视配置要求高).apk
- 免费计算机毕业设计-基于JavaEE的医院病历管理系统设计与实现(包含论文+源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页