MWZoomingScrollView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // ZoomingScrollView.m
  3. // MWPhotoBrowser
  4. //
  5. // Created by Michael Waterfall on 14/10/2010.
  6. // Copyright 2010 d3i. All rights reserved.
  7. //
  8. #import "MWCommon.h"
  9. #import "MWZoomingScrollView.h"
  10. #import "MWPhotoBrowser.h"
  11. #import "MWPhoto.h"
  12. #import "MWPhotoBrowserPrivate.h"
  13. #import "UIImage+MWPhotoBrowser.h"
  14. // Private methods and properties
  15. @interface MWZoomingScrollView () {
  16. MWPhotoBrowser __weak *_photoBrowser;
  17. MWTapDetectingView *_tapView; // for background taps
  18. MWTapDetectingImageView *_photoImageView;
  19. UIImageView *_loadingError;
  20. }
  21. @end
  22. @implementation MWZoomingScrollView
  23. - (id)initWithPhotoBrowser:(MWPhotoBrowser *)browser {
  24. if ((self = [super init])) {
  25. // Setup
  26. _index = NSUIntegerMax;
  27. _photoBrowser = browser;
  28. // Tap view for background
  29. _tapView = [[MWTapDetectingView alloc] initWithFrame:self.bounds];
  30. _tapView.tapDelegate = self;
  31. _tapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  32. _tapView.backgroundColor = [UIColor whiteColor]; //TWS
  33. [self addSubview:_tapView];
  34. // Image view
  35. _photoImageView = [[MWTapDetectingImageView alloc] initWithFrame:CGRectZero];
  36. _photoImageView.tapDelegate = self;
  37. _photoImageView.contentMode = UIViewContentModeCenter;
  38. _photoImageView.backgroundColor = [UIColor whiteColor]; //TWS
  39. [self addSubview:_photoImageView];
  40. // Setup
  41. self.backgroundColor = [UIColor whiteColor]; //TWS
  42. self.delegate = self;
  43. self.showsHorizontalScrollIndicator = NO;
  44. self.showsVerticalScrollIndicator = NO;
  45. self.decelerationRate = UIScrollViewDecelerationRateFast;
  46. self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  47. }
  48. return self;
  49. }
  50. - (void)dealloc {
  51. [[NSNotificationCenter defaultCenter] removeObserver:self];
  52. }
  53. - (void)prepareForReuse {
  54. [self hideImageFailure];
  55. self.photo = nil;
  56. self.captionView = nil;
  57. self.selectedButton = nil;
  58. self.playButton = nil;
  59. _photoImageView.hidden = NO;
  60. _photoImageView.image = nil;
  61. _index = NSUIntegerMax;
  62. }
  63. - (BOOL)displayingVideo {
  64. return [_photo respondsToSelector:@selector(isVideo)] && _photo.isVideo;
  65. }
  66. - (void)setImageHidden:(BOOL)hidden {
  67. _photoImageView.hidden = hidden;
  68. }
  69. #pragma mark - Image
  70. - (void)setPhoto:(id<MWPhoto>)photo {
  71. // Cancel any loading on old photo
  72. if (_photo && photo == nil) {
  73. if ([_photo respondsToSelector:@selector(cancelAnyLoading)]) {
  74. [_photo cancelAnyLoading];
  75. }
  76. }
  77. _photo = photo;
  78. UIImage *img = [_photoBrowser imageForPhoto:_photo];
  79. if (img) {
  80. [self displayImage];
  81. } else {
  82. // Will be loading so show loading
  83. [self showLoadingIndicator];
  84. }
  85. }
  86. // Get and display image
  87. - (void)displayImage {
  88. if (_photo && _photoImageView.image == nil) {
  89. // Reset
  90. self.maximumZoomScale = 1;
  91. self.minimumZoomScale = 1;
  92. self.zoomScale = 1;
  93. self.contentSize = CGSizeMake(0, 0);
  94. // Get image from browser as it handles ordering of fetching
  95. UIImage *img = [_photoBrowser imageForPhoto:_photo];
  96. if (img) {
  97. // Set image
  98. _photoImageView.image = img;
  99. _photoImageView.hidden = NO;
  100. // Setup photo frame
  101. CGRect photoImageViewFrame;
  102. photoImageViewFrame.origin = CGPointZero;
  103. photoImageViewFrame.size = img.size;
  104. _photoImageView.frame = photoImageViewFrame;
  105. self.contentSize = photoImageViewFrame.size;
  106. // Set zoom to minimum zoom
  107. [self setMaxMinZoomScalesForCurrentBounds];
  108. } else {
  109. // Show image failure
  110. [self displayImageFailure];
  111. }
  112. [self setNeedsLayout];
  113. }
  114. }
  115. // Image failed so just show black!
  116. - (void)displayImageFailure {
  117. _photoImageView.image = nil;
  118. // Show if image is not empty
  119. if (![_photo respondsToSelector:@selector(emptyImage)] || !_photo.emptyImage) {
  120. if (!_loadingError) {
  121. _loadingError = [UIImageView new];
  122. _loadingError.image = [UIImage imageForResourcePath:@"ImageError" ofType:@"png" inBundle:[NSBundle bundleForClass:[self class]]];
  123. _loadingError.userInteractionEnabled = NO;
  124. _loadingError.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin |
  125. UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
  126. [_loadingError sizeToFit];
  127. [self addSubview:_loadingError];
  128. }
  129. _loadingError.frame = CGRectMake(floorf((self.bounds.size.width - _loadingError.frame.size.width) / 2.),
  130. floorf((self.bounds.size.height - _loadingError.frame.size.height) / 2),
  131. _loadingError.frame.size.width,
  132. _loadingError.frame.size.height);
  133. }
  134. }
  135. - (void)hideImageFailure {
  136. if (_loadingError) {
  137. [_loadingError removeFromSuperview];
  138. _loadingError = nil;
  139. }
  140. }
  141. #pragma mark - Loading Progress
  142. //TWS
  143. - (void)showLoadingIndicator {
  144. self.zoomScale = 0.1;
  145. self.minimumZoomScale = 0.1;
  146. self.maximumZoomScale = 0.1;
  147. [self hideImageFailure];
  148. }
  149. #pragma mark - Setup
  150. - (CGFloat)initialZoomScaleWithMinScale {
  151. CGFloat zoomScale = self.minimumZoomScale;
  152. if (_photoImageView && _photoBrowser.zoomPhotosToFill) {
  153. // Zoom image to fill if the aspect ratios are fairly similar
  154. CGSize boundsSize = self.bounds.size;
  155. CGSize imageSize = _photoImageView.image.size;
  156. CGFloat boundsAR = boundsSize.width / boundsSize.height;
  157. CGFloat imageAR = imageSize.width / imageSize.height;
  158. CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
  159. CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
  160. // Zooms standard portrait images on a 3.5in screen but not on a 4in screen.
  161. if (ABS(boundsAR - imageAR) < 0.17) {
  162. zoomScale = MAX(xScale, yScale);
  163. // Ensure we don't zoom in or out too far, just in case
  164. zoomScale = MIN(MAX(self.minimumZoomScale, zoomScale), self.maximumZoomScale);
  165. }
  166. }
  167. return zoomScale;
  168. }
  169. - (void)setMaxMinZoomScalesForCurrentBounds {
  170. // Reset
  171. self.maximumZoomScale = 1;
  172. self.minimumZoomScale = 1;
  173. self.zoomScale = 1;
  174. // Bail if no image
  175. if (_photoImageView.image == nil) return;
  176. // Reset position
  177. _photoImageView.frame = CGRectMake(0, 0, _photoImageView.frame.size.width, _photoImageView.frame.size.height);
  178. // Sizes
  179. CGSize boundsSize = self.bounds.size;
  180. CGSize imageSize = _photoImageView.image.size;
  181. // Calculate Min
  182. CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise
  183. CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise
  184. CGFloat minScale = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible
  185. // Calculate Max
  186. CGFloat maxScale = 3;
  187. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  188. // Let them go a bit bigger on a bigger screen!
  189. maxScale = 4;
  190. }
  191. // Image is smaller than screen so no zooming!
  192. if (xScale >= 1 && yScale >= 1) {
  193. minScale = 1.0;
  194. }
  195. // Set min/max zoom
  196. self.maximumZoomScale = maxScale;
  197. self.minimumZoomScale = minScale;
  198. // Initial zoom
  199. self.zoomScale = [self initialZoomScaleWithMinScale];
  200. // If we're zooming to fill then centralise
  201. if (self.zoomScale != minScale) {
  202. // Centralise
  203. self.contentOffset = CGPointMake((imageSize.width * self.zoomScale - boundsSize.width) / 2.0,
  204. (imageSize.height * self.zoomScale - boundsSize.height) / 2.0);
  205. }
  206. // Disable scrolling initially until the first pinch to fix issues with swiping on an initally zoomed in photo
  207. self.scrollEnabled = NO;
  208. // If it's a video then disable zooming
  209. if ([self displayingVideo]) {
  210. self.maximumZoomScale = self.zoomScale;
  211. self.minimumZoomScale = self.zoomScale;
  212. }
  213. // Layout
  214. [self setNeedsLayout];
  215. }
  216. #pragma mark - Layout
  217. - (void)layoutSubviews {
  218. // Update tap view frame
  219. _tapView.frame = self.bounds;
  220. // Position indicators (centre does not seem to work!)
  221. if (_loadingError)
  222. _loadingError.frame = CGRectMake(floorf((self.bounds.size.width - _loadingError.frame.size.width) / 2.),
  223. floorf((self.bounds.size.height - _loadingError.frame.size.height) / 2),
  224. _loadingError.frame.size.width,
  225. _loadingError.frame.size.height);
  226. // Super
  227. [super layoutSubviews];
  228. // Center the image as it becomes smaller than the size of the screen
  229. CGSize boundsSize = self.bounds.size;
  230. CGRect frameToCenter = _photoImageView.frame;
  231. // Horizontally
  232. if (frameToCenter.size.width < boundsSize.width) {
  233. frameToCenter.origin.x = floorf((boundsSize.width - frameToCenter.size.width) / 2.0);
  234. } else {
  235. frameToCenter.origin.x = 0;
  236. }
  237. // Vertically
  238. if (frameToCenter.size.height < boundsSize.height) {
  239. frameToCenter.origin.y = floorf((boundsSize.height - frameToCenter.size.height) / 2.0);
  240. } else {
  241. frameToCenter.origin.y = 0;
  242. }
  243. // Center
  244. if (!CGRectEqualToRect(_photoImageView.frame, frameToCenter))
  245. _photoImageView.frame = frameToCenter;
  246. }
  247. #pragma mark - UIScrollViewDelegate
  248. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  249. return _photoImageView;
  250. }
  251. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  252. [_photoBrowser cancelControlHiding];
  253. }
  254. - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
  255. self.scrollEnabled = YES; // reset
  256. [_photoBrowser cancelControlHiding];
  257. }
  258. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  259. [_photoBrowser hideControlsAfterDelay];
  260. }
  261. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  262. [self setNeedsLayout];
  263. [self layoutIfNeeded];
  264. }
  265. #pragma mark - Tap Detection
  266. - (void)handleSingleTap:(CGPoint)touchPoint {
  267. [_photoBrowser performSelector:@selector(toggleControls) withObject:nil afterDelay:0.2];
  268. }
  269. - (void)handleDoubleTap:(CGPoint)touchPoint {
  270. // Dont double tap to zoom if showing a video
  271. if ([self displayingVideo]) {
  272. return;
  273. }
  274. // Cancel any single tap handling
  275. [NSObject cancelPreviousPerformRequestsWithTarget:_photoBrowser];
  276. // Zoom
  277. if (self.zoomScale != self.minimumZoomScale && self.zoomScale != [self initialZoomScaleWithMinScale]) {
  278. // Zoom out
  279. [self setZoomScale:self.minimumZoomScale animated:YES];
  280. } else {
  281. // Zoom in to twice the size
  282. CGFloat newZoomScale = ((self.maximumZoomScale + self.minimumZoomScale) / 2);
  283. CGFloat xsize = self.bounds.size.width / newZoomScale;
  284. CGFloat ysize = self.bounds.size.height / newZoomScale;
  285. [self zoomToRect:CGRectMake(touchPoint.x - xsize/2, touchPoint.y - ysize/2, xsize, ysize) animated:YES];
  286. }
  287. // Delay controls
  288. [_photoBrowser hideControlsAfterDelay];
  289. }
  290. // Image View
  291. - (void)imageView:(UIImageView *)imageView singleTapDetected:(UITouch *)touch {
  292. [self handleSingleTap:[touch locationInView:imageView]];
  293. }
  294. - (void)imageView:(UIImageView *)imageView doubleTapDetected:(UITouch *)touch {
  295. [self handleDoubleTap:[touch locationInView:imageView]];
  296. }
  297. // Background View
  298. - (void)view:(UIView *)view singleTapDetected:(UITouch *)touch {
  299. // Translate touch location to image view location
  300. CGFloat touchX = [touch locationInView:view].x;
  301. CGFloat touchY = [touch locationInView:view].y;
  302. touchX *= 1/self.zoomScale;
  303. touchY *= 1/self.zoomScale;
  304. touchX += self.contentOffset.x;
  305. touchY += self.contentOffset.y;
  306. [self handleSingleTap:CGPointMake(touchX, touchY)];
  307. }
  308. - (void)view:(UIView *)view doubleTapDetected:(UITouch *)touch {
  309. // Translate touch location to image view location
  310. CGFloat touchX = [touch locationInView:view].x;
  311. CGFloat touchY = [touch locationInView:view].y;
  312. touchX *= 1/self.zoomScale;
  313. touchY *= 1/self.zoomScale;
  314. touchX += self.contentOffset.x;
  315. touchY += self.contentOffset.y;
  316. [self handleDoubleTap:CGPointMake(touchX, touchY)];
  317. }
  318. @end