PagingPhotoView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2016 Google LLC. All rights reserved.
  3. *
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  6. * file except in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under
  11. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. * ANY KIND, either express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #import "GooglePlacesDemos/Samples/PagingPhotoView.h"
  16. /**
  17. * Class to store the image and text views that display the image and attributions.
  18. */
  19. @interface ImageViewAndAttribution : NSObject
  20. @property(nonatomic, strong) UIImageView *imageView;
  21. @property(nonatomic, strong) UITextView *attributionView;
  22. @end
  23. @implementation ImageViewAndAttribution
  24. @end
  25. @implementation AttributedPhoto
  26. @end
  27. @interface PagingPhotoView () <UITextViewDelegate>
  28. @end
  29. @implementation PagingPhotoView {
  30. // An array of |ImageViewAndAttribution| objects representing the actual views that are
  31. // being displayed.
  32. NSMutableArray *_photoImageViews;
  33. // Whether we should update the image and attribution view frames on the next |layoutSubviews|
  34. // call. This should be set to YES whenever the frame is updated or the photos change.
  35. BOOL _imageLayoutUpdateNeeded;
  36. }
  37. - (instancetype)initWithFrame:(CGRect)frame {
  38. if ((self = [super initWithFrame:frame])) {
  39. _photoImageViews = [NSMutableArray array];
  40. self.backgroundColor = [UIColor whiteColor];
  41. self.pagingEnabled = YES;
  42. }
  43. return self;
  44. }
  45. - (void)setPhotoList:(NSArray *)photoList {
  46. // First, remove all of the existing image and attribution subviews.
  47. for (ImageViewAndAttribution *photoView in _photoImageViews) {
  48. [photoView.imageView removeFromSuperview];
  49. [photoView.attributionView removeFromSuperview];
  50. }
  51. [_photoImageViews removeAllObjects];
  52. // Add the new images and attributions as subviews.
  53. _photoList = [photoList copy];
  54. for (AttributedPhoto *photo in photoList) {
  55. UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
  56. textView.delegate = self;
  57. textView.editable = NO;
  58. textView.attributedText = photo.attributions;
  59. [self addSubview:textView];
  60. UIImageView *imageView = [[UIImageView alloc] initWithImage:photo.image];
  61. imageView.contentMode = UIViewContentModeScaleAspectFit;
  62. imageView.clipsToBounds = YES;
  63. [self addSubview:imageView];
  64. ImageViewAndAttribution *attributedView = [[ImageViewAndAttribution alloc] init];
  65. attributedView.imageView = imageView;
  66. attributedView.attributionView = textView;
  67. [_photoImageViews addObject:attributedView];
  68. }
  69. [self updateContentSize];
  70. _imageLayoutUpdateNeeded = YES;
  71. }
  72. - (void)setFrame:(CGRect)frame {
  73. _imageLayoutUpdateNeeded = YES;
  74. // We want to make sure that we are still scrolled to the same photo when the frame changes.
  75. // Measure the current content offset and scroll to the same fraction along the content after the
  76. // frame change.
  77. CGFloat scrollOffsetFraction = 0;
  78. if (self.contentSize.width != 0) {
  79. scrollOffsetFraction = self.contentOffset.x / self.contentSize.width;
  80. }
  81. [super setFrame:frame];
  82. [UIView performWithoutAnimation:^{
  83. [self updateContentSize];
  84. self.contentOffset =
  85. CGPointMake(scrollOffsetFraction * self.contentSize.width, -self.contentInset.top);
  86. }];
  87. }
  88. - (void)layoutSubviews {
  89. [super layoutSubviews];
  90. if (_imageLayoutUpdateNeeded) {
  91. [self layoutImages];
  92. _imageLayoutUpdateNeeded = NO;
  93. // Re-adjust the content offset to ensure the photos are aligned properly horizontally.
  94. if (self.contentSize.width != 0) {
  95. CGFloat scrollOffset =
  96. (CGFloat)round((self.contentOffset.x / self.contentSize.width) * 10.0f) / 10.0f;
  97. self.contentOffset =
  98. CGPointMake(scrollOffset * self.contentSize.width, -self.contentInset.top);
  99. }
  100. }
  101. }
  102. #pragma mark - UITextViewDelegate
  103. - (BOOL)textView:(UITextView *)textView
  104. shouldInteractWithURL:(NSURL *)url
  105. inRange:(NSRange)characterRange {
  106. // Make links clickable.
  107. return YES;
  108. }
  109. #pragma mark - Helper methods
  110. /**
  111. * Update the content size of the scroll view based on the number of photos and the view's width.
  112. * This should be called whenever the frame changes or the number of photos has changed.
  113. */
  114. - (void)updateContentSize {
  115. CGRect insetBounds = UIEdgeInsetsInsetRect(self.bounds, self.contentInset);
  116. CGFloat usableScrollViewHeight = insetBounds.size.height;
  117. self.contentSize =
  118. CGSizeMake(_photoImageViews.count * self.frame.size.width, usableScrollViewHeight);
  119. }
  120. /**
  121. * Updates the frames of the images and attributions.
  122. */
  123. - (void)layoutImages {
  124. CGFloat contentWidth = 0;
  125. CGFloat scrollViewWidth = self.bounds.size.width;
  126. CGRect insetBounds = UIEdgeInsetsInsetRect(self.bounds, self.contentInset);
  127. CGFloat usableScrollViewHeight = insetBounds.size.height;
  128. // Lay out the images one after the other horizontally.
  129. for (ImageViewAndAttribution *attributedImageView in _photoImageViews) {
  130. UITextView *attributionView = attributedImageView.attributionView;
  131. UIImageView *imageView = attributedImageView.imageView;
  132. [attributionView sizeToFit];
  133. CGFloat attributionHeight = attributionView.frame.size.height;
  134. CGFloat imageHeight = usableScrollViewHeight - attributionHeight;
  135. CGFloat safeAreaX = 0.0f;
  136. // Take into account the safe areas of the device screen and do not use that space for the
  137. // attribution text.
  138. imageHeight -= self.safeAreaInsets.bottom;
  139. safeAreaX = self.safeAreaInsets.left;
  140. // Put the attribution view aligned to the same left edge as the photo, in the bottom left
  141. // corner of the screen.
  142. attributionView.frame = CGRectMake(contentWidth + safeAreaX, imageHeight,
  143. scrollViewWidth - (2 * safeAreaX), attributionHeight);
  144. imageView.frame = CGRectMake(contentWidth, 0, scrollViewWidth, imageHeight);
  145. contentWidth += imageView.frame.size.width;
  146. }
  147. }
  148. @end