RSKImageScrollView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. File: RSKImageScrollView.m
  3. Abstract: Centers image within the scroll view and configures image sizing and display.
  4. Version: 1.3 modified by Ruslan Skorb on 8/24/14.
  5. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  6. Inc. ("Apple") in consideration of your agreement to the following
  7. terms, and your use, installation, modification or redistribution of
  8. this Apple software constitutes acceptance of these terms. If you do
  9. not agree with these terms, please do not use, install, modify or
  10. redistribute this Apple software.
  11. In consideration of your agreement to abide by the following terms, and
  12. subject to these terms, Apple grants you a personal, non-exclusive
  13. license, under Apple's copyrights in this original Apple software (the
  14. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  15. Software, with or without modifications, in source and/or binary forms;
  16. provided that if you redistribute the Apple Software in its entirety and
  17. without modifications, you must retain this notice and the following
  18. text and disclaimers in all such redistributions of the Apple Software.
  19. Neither the name, trademarks, service marks or logos of Apple Inc. may
  20. be used to endorse or promote products derived from the Apple Software
  21. without specific prior written permission from Apple. Except as
  22. expressly stated in this notice, no other rights or licenses, express or
  23. implied, are granted by Apple herein, including but not limited to any
  24. patent rights that may be infringed by your derivative works or by other
  25. works in which the Apple Software may be incorporated.
  26. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  27. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  28. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  29. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  30. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  31. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  32. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  35. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  36. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  37. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  38. POSSIBILITY OF SUCH DAMAGE.
  39. Copyright (C) 2012 Apple Inc. All Rights Reserved.
  40. */
  41. #import <Foundation/Foundation.h>
  42. #import "RSKImageScrollView.h"
  43. #import "RSKImageScrollViewDelegate.h"
  44. #pragma mark -
  45. @interface RSKImageScrollView () <UIScrollViewDelegate>
  46. {
  47. CGSize _imageSize;
  48. CGPoint _pointToCenterAfterResize;
  49. CGFloat _scaleToRestoreAfterResize;
  50. }
  51. @end
  52. @implementation RSKImageScrollView
  53. - (id)initWithFrame:(CGRect)frame
  54. {
  55. self = [super initWithFrame:frame];
  56. if (self)
  57. {
  58. _aspectFill = NO;
  59. self.showsVerticalScrollIndicator = NO;
  60. self.showsHorizontalScrollIndicator = NO;
  61. self.scrollsToTop = NO;
  62. self.decelerationRate = UIScrollViewDecelerationRateFast;
  63. self.delegate = self;
  64. }
  65. return self;
  66. }
  67. - (void)didAddSubview:(UIView *)subview
  68. {
  69. [super didAddSubview:subview];
  70. [self centerZoomView];
  71. }
  72. - (void)setAspectFill:(BOOL)aspectFill
  73. {
  74. if (_aspectFill != aspectFill) {
  75. _aspectFill = aspectFill;
  76. if (_zoomView) {
  77. [self setMaxMinZoomScalesForCurrentBounds];
  78. if (self.zoomScale < self.minimumZoomScale) {
  79. self.zoomScale = self.minimumZoomScale;
  80. }
  81. }
  82. }
  83. }
  84. - (void)setFrame:(CGRect)frame
  85. {
  86. BOOL sizeChanging = !CGSizeEqualToSize(frame.size, self.frame.size);
  87. if (sizeChanging) {
  88. [self prepareToResize];
  89. }
  90. [super setFrame:frame];
  91. if (sizeChanging) {
  92. [self recoverFromResizing];
  93. }
  94. [self centerZoomView];
  95. }
  96. #pragma mark - UIScrollViewDelegate
  97. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  98. {
  99. return _zoomView;
  100. }
  101. - (void)scrollViewDidZoom:(__unused UIScrollView *)scrollView
  102. {
  103. [self centerZoomView];
  104. }
  105. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  106. {
  107. [self.imageScrollViewDelegate imageScrollViewWillBeginDragging];
  108. }
  109. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  110. {
  111. [self.imageScrollViewDelegate imageScrollViewDidEndDragging:decelerate];
  112. }
  113. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  114. {
  115. [self.imageScrollViewDelegate imageScrollViewDidEndDecelerating];
  116. }
  117. - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
  118. {
  119. [self.imageScrollViewDelegate imageScrollViewWillBeginZooming];
  120. }
  121. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
  122. {
  123. [self.imageScrollViewDelegate imageScrollViewDidEndZooming];
  124. }
  125. #pragma mark - Center zoomView within scrollView
  126. - (void)centerZoomView
  127. {
  128. // center zoomView as it becomes smaller than the size of the screen
  129. CGFloat top = 0;
  130. CGFloat left = 0;
  131. // center vertically
  132. if (self.contentSize.height < CGRectGetHeight(self.bounds)) {
  133. top = (CGRectGetHeight(self.bounds) - self.contentSize.height) * 0.5f;
  134. }
  135. // center horizontally
  136. if (self.contentSize.width < CGRectGetWidth(self.bounds)) {
  137. left = (CGRectGetWidth(self.bounds) - self.contentSize.width) * 0.5f;
  138. }
  139. self.contentInset = UIEdgeInsetsMake(top, left, top, left);
  140. }
  141. #pragma mark - Configure scrollView to display new image
  142. - (void)displayImage:(UIImage *)image
  143. {
  144. // clear view for the previous image
  145. [_zoomView removeFromSuperview];
  146. _zoomView = nil;
  147. // reset our zoomScale to 1.0 before doing any further calculations
  148. self.zoomScale = 1.0;
  149. // make views to display the new image
  150. _zoomView = [[UIImageView alloc] initWithImage:image];
  151. [self addSubview:_zoomView];
  152. [self configureForImageSize:image.size];
  153. }
  154. - (void)configureForImageSize:(CGSize)imageSize
  155. {
  156. _imageSize = imageSize;
  157. self.contentSize = imageSize;
  158. [self setMaxMinZoomScalesForCurrentBounds];
  159. [self setInitialZoomScale];
  160. [self setInitialContentOffset];
  161. self.contentInset = UIEdgeInsetsZero;
  162. }
  163. - (void)setMaxMinZoomScalesForCurrentBounds
  164. {
  165. CGSize boundsSize = self.bounds.size;
  166. // calculate min/max zoomscale
  167. CGFloat xScale = boundsSize.width / _imageSize.width; // the scale needed to perfectly fit the image width-wise
  168. CGFloat yScale = boundsSize.height / _imageSize.height; // the scale needed to perfectly fit the image height-wise
  169. CGFloat minScale;
  170. if (!self.aspectFill) {
  171. minScale = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible
  172. } else {
  173. minScale = MAX(xScale, yScale); // use maximum of these to allow the image to fill the screen
  174. }
  175. CGFloat maxScale = MAX(xScale, yScale);
  176. // Image must fit/fill the screen, even if its size is smaller.
  177. CGFloat xImageScale = maxScale*_imageSize.width / boundsSize.width;
  178. CGFloat yImageScale = maxScale*_imageSize.height / boundsSize.height;
  179. CGFloat maxImageScale = MAX(xImageScale, yImageScale);
  180. maxImageScale = MAX(minScale, maxImageScale);
  181. maxScale = MAX(maxScale, maxImageScale);
  182. // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.)
  183. if (minScale > maxScale) {
  184. minScale = maxScale;
  185. }
  186. self.maximumZoomScale = maxScale;
  187. self.minimumZoomScale = minScale;
  188. }
  189. - (void)setInitialZoomScale
  190. {
  191. CGSize boundsSize = self.bounds.size;
  192. CGFloat xScale = boundsSize.width / _imageSize.width; // the scale needed to perfectly fit the image width-wise
  193. CGFloat yScale = boundsSize.height / _imageSize.height; // the scale needed to perfectly fit the image height-wise
  194. CGFloat scale = MAX(xScale, yScale);
  195. self.zoomScale = scale;
  196. }
  197. - (void)setInitialContentOffset
  198. {
  199. CGSize boundsSize = self.bounds.size;
  200. CGRect frameToCenter = self.zoomView.frame;
  201. CGPoint contentOffset;
  202. if (CGRectGetWidth(frameToCenter) > boundsSize.width) {
  203. contentOffset.x = (CGRectGetWidth(frameToCenter) - boundsSize.width) * 0.5f;
  204. } else {
  205. contentOffset.x = 0;
  206. }
  207. if (CGRectGetHeight(frameToCenter) > boundsSize.height) {
  208. contentOffset.y = (CGRectGetHeight(frameToCenter) - boundsSize.height) * 0.5f;
  209. } else {
  210. contentOffset.y = 0;
  211. }
  212. [self setContentOffset:contentOffset];
  213. }
  214. #pragma mark -
  215. #pragma mark Methods called during rotation to preserve the zoomScale and the visible portion of the image
  216. #pragma mark - Rotation support
  217. - (void)prepareToResize
  218. {
  219. if (_zoomView == nil) {
  220. return;
  221. }
  222. CGPoint boundsCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  223. _pointToCenterAfterResize = [self convertPoint:boundsCenter toView:self.zoomView];
  224. _scaleToRestoreAfterResize = self.zoomScale;
  225. // If we're at the minimum zoom scale, preserve that by returning 0, which will be converted to the minimum
  226. // allowable scale when the scale is restored.
  227. if (_scaleToRestoreAfterResize <= self.minimumZoomScale + FLT_EPSILON)
  228. _scaleToRestoreAfterResize = 0;
  229. }
  230. - (void)recoverFromResizing
  231. {
  232. if (_zoomView == nil) {
  233. return;
  234. }
  235. [self setMaxMinZoomScalesForCurrentBounds];
  236. // Step 1: restore zoom scale, first making sure it is within the allowable range.
  237. CGFloat maxZoomScale = MAX(self.minimumZoomScale, _scaleToRestoreAfterResize);
  238. self.zoomScale = MIN(self.maximumZoomScale, maxZoomScale);
  239. // Step 2: restore center point, first making sure it is within the allowable range.
  240. // 2a: convert our desired center point back to our own coordinate space
  241. CGPoint boundsCenter = [self convertPoint:_pointToCenterAfterResize fromView:self.zoomView];
  242. // 2b: calculate the content offset that would yield that center point
  243. CGPoint offset = CGPointMake(boundsCenter.x - self.bounds.size.width / 2.0,
  244. boundsCenter.y - self.bounds.size.height / 2.0);
  245. // 2c: restore offset, adjusted to be within the allowable range
  246. CGPoint maxOffset = [self maximumContentOffset];
  247. CGPoint minOffset = [self minimumContentOffset];
  248. CGFloat realMaxOffset = MIN(maxOffset.x, offset.x);
  249. offset.x = MAX(minOffset.x, realMaxOffset);
  250. realMaxOffset = MIN(maxOffset.y, offset.y);
  251. offset.y = MAX(minOffset.y, realMaxOffset);
  252. self.contentOffset = offset;
  253. }
  254. - (CGPoint)maximumContentOffset
  255. {
  256. CGSize contentSize = self.contentSize;
  257. CGSize boundsSize = self.bounds.size;
  258. return CGPointMake(contentSize.width - boundsSize.width, contentSize.height - boundsSize.height);
  259. }
  260. - (CGPoint)minimumContentOffset
  261. {
  262. return CGPointZero;
  263. }
  264. @end