RSKImageScrollView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. File: RSKImageScrollView.m
  3. Abstract: Centers image within the scroll view and configures image sizing and display.
  4. Version: 1.5 modified by Ruslan Skorb on 11/26/24.
  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. Copyright (C) 2014-present Ruslan Skorb. All Rights Reserved.
  41. */
  42. #import <Foundation/Foundation.h>
  43. #import "RSKImageScrollView.h"
  44. #import "RSKImageScrollViewDelegate.h"
  45. #pragma mark -
  46. @interface RSKImageScrollView () <UIScrollViewDelegate>
  47. {
  48. CGSize _imageSize;
  49. UIImageView *_imageView;
  50. CGPoint _pointToCenterAfterResize;
  51. CGFloat _scaleToRestoreAfterResize;
  52. }
  53. @end
  54. @implementation RSKImageScrollView
  55. - (id)initWithFrame:(CGRect)frame
  56. {
  57. self = [super initWithFrame:frame];
  58. if (self)
  59. {
  60. _aspectFill = NO;
  61. _imageView = [[UIImageView alloc] init];
  62. self.showsVerticalScrollIndicator = NO;
  63. self.showsHorizontalScrollIndicator = NO;
  64. self.scrollsToTop = NO;
  65. self.decelerationRate = UIScrollViewDecelerationRateFast;
  66. self.delegate = self;
  67. [self addSubview:_imageView];
  68. }
  69. return self;
  70. }
  71. - (void)didAddSubview:(UIView *)subview
  72. {
  73. [super didAddSubview:subview];
  74. [self centerImageView];
  75. }
  76. - (void)setAspectFill:(BOOL)aspectFill
  77. {
  78. if (_aspectFill != aspectFill) {
  79. _aspectFill = aspectFill;
  80. if (_imageView.image) {
  81. [self setMaxMinZoomScalesForCurrentBounds];
  82. if (self.zoomScale < self.minimumZoomScale) {
  83. self.zoomScale = self.minimumZoomScale;
  84. } else if (self.zoomScale > self.maximumZoomScale) {
  85. self.zoomScale = self.maximumZoomScale;
  86. }
  87. }
  88. }
  89. }
  90. - (UIImage *)image
  91. {
  92. return _imageView.image;
  93. }
  94. - (void)setImage:(UIImage *)image
  95. {
  96. _imageView.image = image;
  97. if (CGSizeEqualToSize(_imageSize, CGSizeZero)) {
  98. self.imageSize = image.size;
  99. }
  100. }
  101. - (UIColor *)imageViewBackgroundColor
  102. {
  103. return _imageView.backgroundColor;
  104. }
  105. - (void)setImageViewBackgroundColor:(UIColor *)imageViewBackgroundColor
  106. {
  107. _imageView.backgroundColor = imageViewBackgroundColor;
  108. }
  109. - (id<UICoordinateSpace>)imageViewCoordinateSpace
  110. {
  111. return [_imageView coordinateSpace];
  112. }
  113. - (CGRect)imageViewFrame
  114. {
  115. return _imageView.frame;
  116. }
  117. - (void)setImageSize:(CGSize)imageSize
  118. {
  119. _imageSize = imageSize;
  120. self.zoomScale = 1.0f;
  121. _imageView.frame = CGRectMake(0.0f, 0.0f, imageSize.width, imageSize.height);
  122. self.contentSize = imageSize;
  123. [self setMaxMinZoomScalesForCurrentBounds];
  124. [self setInitialZoomScale];
  125. [self setInitialContentOffset];
  126. [self centerImageView];
  127. }
  128. - (void)setInitialZoomScaleAndContentOffsetAndCenterImageView
  129. {
  130. [self setInitialZoomScale];
  131. [self setInitialContentOffset];
  132. [self centerImageView];
  133. }
  134. - (void)setInitialZoomScaleAndContentOffsetAnimated:(BOOL)animated
  135. {
  136. if (animated) {
  137. UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationCurveEaseInOut;
  138. [UIView animateWithDuration:0.4f delay:0.0f options:options animations:^{
  139. [self setInitialZoomScaleAndContentOffsetAndCenterImageView];
  140. } completion:nil];
  141. } else {
  142. [self setInitialZoomScaleAndContentOffsetAndCenterImageView];
  143. }
  144. }
  145. - (void)setFrame:(CGRect)frame
  146. {
  147. if (CGSizeEqualToSize(self.contentSize, CGSizeZero)) {
  148. [super setFrame:frame];
  149. return;
  150. }
  151. BOOL sizeChanging = !CGSizeEqualToSize(frame.size, self.frame.size);
  152. if (sizeChanging) {
  153. [self prepareToResize];
  154. }
  155. [super setFrame:frame];
  156. if (sizeChanging) {
  157. [self recoverFromResizing];
  158. }
  159. [self centerImageView];
  160. }
  161. - (void)zoomToLocation:(CGPoint)location animated:(BOOL)animated
  162. {
  163. CGPoint locationInImageView = [_imageView convertPoint:location fromView:self];
  164. CGSize size = CGSizeMake(self.bounds.size.width / MIN(self.zoomScale * 5.0f, self.maximumZoomScale),
  165. self.bounds.size.height / MIN(self.zoomScale * 5.0f, self.maximumZoomScale));
  166. CGPoint origin = CGPointMake(locationInImageView.x - size.width * 0.5f,
  167. locationInImageView.y - size.height * 0.5f);
  168. CGRect rect = CGRectMake(origin.x, origin.y, size.width, size.height);
  169. [self zoomToRect:rect animated:animated];
  170. }
  171. #pragma mark - UIScrollViewDelegate
  172. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  173. {
  174. return _imageView;
  175. }
  176. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  177. {
  178. if ([self.imageScrollViewDelegate respondsToSelector:@selector(imageScrollViewDidScroll)]) {
  179. [self.imageScrollViewDelegate imageScrollViewDidScroll];
  180. }
  181. }
  182. - (void)scrollViewDidZoom:(__unused UIScrollView *)scrollView
  183. {
  184. [self centerImageView];
  185. }
  186. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  187. {
  188. if ([self.imageScrollViewDelegate respondsToSelector:@selector(imageScrollViewWillBeginDragging)]) {
  189. [self.imageScrollViewDelegate imageScrollViewWillBeginDragging];
  190. }
  191. }
  192. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  193. {
  194. if ([self.imageScrollViewDelegate respondsToSelector:@selector(imageScrollViewDidEndDragging:)]) {
  195. [self.imageScrollViewDelegate imageScrollViewDidEndDragging:decelerate];
  196. }
  197. }
  198. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  199. {
  200. if ([self.imageScrollViewDelegate respondsToSelector:@selector(imageScrollViewDidEndDecelerating)]) {
  201. [self.imageScrollViewDelegate imageScrollViewDidEndDecelerating];
  202. }
  203. }
  204. - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
  205. {
  206. if ([self.imageScrollViewDelegate respondsToSelector:@selector(imageScrollViewWillBeginZooming)]) {
  207. [self.imageScrollViewDelegate imageScrollViewWillBeginZooming];
  208. }
  209. }
  210. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
  211. {
  212. if ([self.imageScrollViewDelegate respondsToSelector:@selector(imageScrollViewDidEndZooming)]) {
  213. [self.imageScrollViewDelegate imageScrollViewDidEndZooming];
  214. }
  215. }
  216. #pragma mark - Center imageView within scrollView
  217. - (void)centerImageView
  218. {
  219. // center imageView as it becomes smaller than the size of the screen
  220. CGFloat top = 0.0f;
  221. CGFloat left = 0.0f;
  222. // center vertically
  223. if (self.contentSize.height < CGRectGetHeight(self.bounds)) {
  224. top = (CGRectGetHeight(self.bounds) - self.contentSize.height) * 0.5f;
  225. }
  226. // center horizontally
  227. if (self.contentSize.width < CGRectGetWidth(self.bounds)) {
  228. left = (CGRectGetWidth(self.bounds) - self.contentSize.width) * 0.5f;
  229. }
  230. UIEdgeInsets contentInset = UIEdgeInsetsMake(top, left, top, left);
  231. if (!UIEdgeInsetsEqualToEdgeInsets(self.contentInset, contentInset)) {
  232. self.contentInset = contentInset;
  233. }
  234. }
  235. #pragma mark - Configure scrollView to display new image
  236. - (void)setMaxMinZoomScalesForCurrentBounds
  237. {
  238. if (CGSizeEqualToSize(self.bounds.size, CGSizeZero)) {
  239. return;
  240. }
  241. if (CGSizeEqualToSize(_imageSize, CGSizeZero)) {
  242. self.maximumZoomScale = 1.0f;
  243. self.minimumZoomScale = 1.0f;
  244. return;
  245. }
  246. CGSize boundsSize = self.bounds.size;
  247. // calculate min/max zoomscale
  248. CGFloat xScale = boundsSize.width / _imageSize.width; // the scale needed to perfectly fit the image width-wise
  249. CGFloat yScale = boundsSize.height / _imageSize.height; // the scale needed to perfectly fit the image height-wise
  250. CGFloat minScale;
  251. if (_aspectFill) {
  252. minScale = MAX(xScale, yScale); // use maximum of these to allow the image to fill the screen
  253. } else {
  254. minScale = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible
  255. }
  256. CGFloat maxScale = MAX(xScale, yScale);
  257. // Image must fit/fill the screen, even if its size is smaller.
  258. CGFloat xImageScale = maxScale * _imageSize.width / boundsSize.width;
  259. CGFloat yImageScale = maxScale * _imageSize.height / boundsSize.height;
  260. CGFloat maxImageScale = MAX(xImageScale, yImageScale);
  261. maxImageScale = MAX(minScale, maxImageScale);
  262. maxScale = MAX(maxScale, maxImageScale);
  263. // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.)
  264. if (minScale > maxScale) {
  265. minScale = maxScale;
  266. }
  267. self.maximumZoomScale = maxScale;
  268. self.minimumZoomScale = minScale;
  269. }
  270. - (void)setInitialZoomScale
  271. {
  272. if (self.zoomScale != self.minimumZoomScale) {
  273. self.zoomScale = self.minimumZoomScale;
  274. }
  275. }
  276. - (void)setInitialContentOffset
  277. {
  278. CGSize boundsSize = self.bounds.size;
  279. CGRect frameToCenter = _imageView.frame;
  280. CGPoint contentOffset = self.contentOffset;
  281. if (CGRectGetWidth(frameToCenter) > boundsSize.width) {
  282. contentOffset.x = (CGRectGetWidth(frameToCenter) - boundsSize.width) * 0.5f;
  283. }
  284. if (CGRectGetHeight(frameToCenter) > boundsSize.height) {
  285. contentOffset.y = (CGRectGetHeight(frameToCenter) - boundsSize.height) * 0.5f;
  286. }
  287. if (!CGPointEqualToPoint(self.contentOffset, contentOffset)) {
  288. self.contentOffset = contentOffset;
  289. }
  290. }
  291. #pragma mark -
  292. #pragma mark Methods called during rotation to preserve the zoomScale and the visible portion of the image
  293. #pragma mark - Rotation support
  294. - (void)prepareToResize
  295. {
  296. if (_imageView == nil) {
  297. return;
  298. }
  299. CGPoint boundsCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
  300. _pointToCenterAfterResize = [self convertPoint:boundsCenter toView:_imageView];
  301. _scaleToRestoreAfterResize = self.zoomScale;
  302. // If we're at the minimum zoom scale, preserve that by returning 0, which will be converted to the minimum
  303. // allowable scale when the scale is restored.
  304. if (_scaleToRestoreAfterResize <= self.minimumZoomScale + FLT_EPSILON)
  305. _scaleToRestoreAfterResize = 0;
  306. }
  307. - (void)recoverFromResizing
  308. {
  309. if (_imageView == nil) {
  310. return;
  311. }
  312. [self setMaxMinZoomScalesForCurrentBounds];
  313. // Step 1: restore zoom scale, first making sure it is within the allowable range.
  314. CGFloat maxZoomScale = MAX(self.minimumZoomScale, _scaleToRestoreAfterResize);
  315. self.zoomScale = MIN(self.maximumZoomScale, maxZoomScale);
  316. // Step 2: restore center point, first making sure it is within the allowable range.
  317. // 2a: convert our desired center point back to our own coordinate space
  318. CGPoint boundsCenter = [self convertPoint:_pointToCenterAfterResize fromView:_imageView];
  319. // 2b: calculate the content offset that would yield that center point
  320. CGPoint offset = CGPointMake(boundsCenter.x - self.bounds.size.width * 0.5f,
  321. boundsCenter.y - self.bounds.size.height * 0.5f);
  322. // 2c: restore offset, adjusted to be within the allowable range
  323. CGPoint maxOffset = [self maximumContentOffset];
  324. CGPoint minOffset = [self minimumContentOffset];
  325. CGFloat realMaxOffset = MIN(maxOffset.x, offset.x);
  326. offset.x = MAX(minOffset.x, realMaxOffset);
  327. realMaxOffset = MIN(maxOffset.y, offset.y);
  328. offset.y = MAX(minOffset.y, realMaxOffset);
  329. if (self.contentSize.height < self.bounds.size.height) {
  330. offset.y = -(self.bounds.size.height - self.contentSize.height) * 0.5f;
  331. }
  332. if (self.contentSize.width < self.bounds.size.width) {
  333. offset.x = -(self.bounds.size.width - self.contentSize.width) * 0.5f;
  334. }
  335. self.contentOffset = offset;
  336. }
  337. - (CGPoint)maximumContentOffset
  338. {
  339. CGSize contentSize = self.contentSize;
  340. CGSize boundsSize = self.bounds.size;
  341. return CGPointMake(contentSize.width - boundsSize.width, contentSize.height - boundsSize.height);
  342. }
  343. - (CGPoint)minimumContentOffset
  344. {
  345. return CGPointZero;
  346. }
  347. @end