RITLPhotosBrowseImageCell.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // RITLPhotoBrowerCell.m
  3. // RITLPhotoDemo
  4. //
  5. // Created by YueWen on 2016/12/29.
  6. // Copyright © 2017年 YueWen. All rights reserved.
  7. //
  8. #import "RITLPhotosBrowseImageCell.h"
  9. #import "UICollectionViewCell+RITLPhotosAsset.h"
  10. #import "RITLKit.h"
  11. #import "Masonry.h"
  12. @interface RITLPhotosBrowseImageCell ()<UIScrollViewDelegate>
  13. /// @brief 是否已经缩放的标志位
  14. @property (nonatomic, assign)BOOL isScale;
  15. /// @brief 底部负责滚动的滚动视图
  16. @property (strong, nonatomic, readwrite) IBOutlet UIScrollView *bottomScrollView;
  17. //手势
  18. @property (nonatomic, strong) UITapGestureRecognizer * simpleTapGesture;
  19. @property (nonatomic, strong) UITapGestureRecognizer * doubleTapGesture;
  20. /// @brief 缩放比例
  21. @property (nonatomic, assign) CGFloat minScaleZoome;
  22. @property (nonatomic, assign) CGFloat maxScaleZoome;
  23. @end
  24. @implementation RITLPhotosBrowseImageCell
  25. -(void)dealloc
  26. {
  27. }
  28. -(instancetype)initWithFrame:(CGRect)frame
  29. {
  30. if (self = [super initWithFrame:frame]) {
  31. [self browerCellLoad];
  32. }
  33. return self;
  34. }
  35. - (void)browerCellLoad
  36. {
  37. self.minScaleZoome = 1.0f;
  38. self.maxScaleZoome = 2.0f;
  39. [self createBottomScrollView];
  40. [self createImageView];
  41. [self createDoubleTapGesture];
  42. [self createSimpleTapGesture];
  43. }
  44. -(void)prepareForReuse
  45. {
  46. _imageView.image = nil;
  47. _bottomScrollView.zoomScale = 1.0f;
  48. }
  49. #pragma mark - create Subviews
  50. - (void)createBottomScrollView
  51. {
  52. if (self.bottomScrollView == nil)
  53. {
  54. self.bottomScrollView = [[UIScrollView alloc]init];
  55. self.bottomScrollView.backgroundColor = [UIColor blackColor];
  56. self.bottomScrollView.delegate = self;
  57. self.bottomScrollView.minimumZoomScale = self.minScaleZoome;
  58. self.bottomScrollView.maximumZoomScale = self.maxScaleZoome;
  59. [self.contentView addSubview:self.bottomScrollView];
  60. __weak typeof(self) weakSelf = self;
  61. //添加约束
  62. [self.bottomScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.edges.equalTo(weakSelf.contentView).offset(0);
  64. }];
  65. }
  66. }
  67. - (void)createImageView
  68. {
  69. if (self.imageView == nil)
  70. {
  71. self.imageView = [[UIImageView alloc]init];
  72. self.imageView.contentMode = UIViewContentModeScaleAspectFit;
  73. [self.bottomScrollView addSubview:self.imageView];
  74. __weak typeof(self) weakSelf = self;
  75. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.edges.equalTo(weakSelf.bottomScrollView);
  77. make.width.equalTo(weakSelf.bottomScrollView.mas_width);
  78. make.height.equalTo(weakSelf.bottomScrollView.mas_height);
  79. }];
  80. }
  81. }
  82. - (void)createDoubleTapGesture
  83. {
  84. if (self.doubleTapGesture == nil)
  85. {
  86. self.doubleTapGesture = [UITapGestureRecognizer new];
  87. self.doubleTapGesture.numberOfTapsRequired = 2;
  88. [self.bottomScrollView addGestureRecognizer:self.doubleTapGesture];
  89. __weak typeof(self) weakSelf = self;
  90. [self.doubleTapGesture gestureRecognizerHandle:^(UIGestureRecognizer * _Nonnull sender) {
  91. __strong typeof(weakSelf) strongSelf = weakSelf;
  92. if (strongSelf.bottomScrollView.zoomScale != 1.0f)
  93. {
  94. [strongSelf.bottomScrollView setZoomScale:1.0f animated:true];
  95. }
  96. else{
  97. //获得Cell的宽度
  98. CGFloat width = strongSelf.frame.size.width;
  99. //触及范围
  100. CGFloat scale = width / strongSelf.maxScaleZoome;
  101. //获取当前的触摸点
  102. CGPoint point = [sender locationInView:strongSelf.imageView];
  103. //对点进行处理
  104. CGFloat originX = MAX(0, point.x - width / scale);
  105. CGFloat originY = MAX(0, point.y - width / scale);
  106. //进行位置的计算
  107. CGRect rect = CGRectMake(originX, originY, width / scale , width / scale);
  108. //进行缩放
  109. [strongSelf.bottomScrollView zoomToRect:rect animated:true];
  110. }
  111. }];
  112. }
  113. }
  114. - (void)createSimpleTapGesture
  115. {
  116. if (self.simpleTapGesture == nil)
  117. {
  118. self.simpleTapGesture = [UITapGestureRecognizer new];
  119. self.simpleTapGesture.numberOfTapsRequired = 1;
  120. [self.simpleTapGesture requireGestureRecognizerToFail:self.doubleTapGesture];
  121. [self.bottomScrollView addGestureRecognizer:self.simpleTapGesture];
  122. [self.simpleTapGesture gestureRecognizerHandle:^(UIGestureRecognizer * _Nonnull sender) {
  123. [NSNotificationCenter.defaultCenter postNotificationName:RITLHorBrowseTooBarChangedHiddenStateNotification object:nil];
  124. }];
  125. }
  126. }
  127. #pragma mark - <UIScrollViewDelegate>
  128. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  129. {
  130. return self.imageView;
  131. }
  132. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale
  133. {
  134. [scrollView setZoomScale:scale animated:true];
  135. }
  136. @end