财神随手记账

MWZoomingScrollView.m 14KB

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