ZoomableImageView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // ZoomableImageView.m
  3. // AIIM
  4. //
  5. // Created by qitewei on 2025/5/21.
  6. //
  7. #import "ZoomableImageView.h"
  8. @implementation ZoomableImageView
  9. - (instancetype)initWithFrame:(CGRect)frame {
  10. self = [super initWithFrame:frame];
  11. if (self) {
  12. [self setupView];
  13. }
  14. return self;
  15. }
  16. - (void)setupView {
  17. // 设置滚动视图属性
  18. self.delegate = self;
  19. self.showsVerticalScrollIndicator = NO;
  20. self.showsHorizontalScrollIndicator = NO;
  21. self.decelerationRate = UIScrollViewDecelerationRateFast;
  22. // 添加图片视图
  23. _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
  24. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  25. _imageView.userInteractionEnabled = YES;
  26. [self addSubview:_imageView];
  27. // 添加手势
  28. UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  29. doubleTap.numberOfTapsRequired = 2;
  30. [_imageView addGestureRecognizer:doubleTap];
  31. }
  32. - (void)setImage:(UIImage *)image {
  33. self.imageView.image = image;
  34. [self resetZoom];
  35. }
  36. - (void)resetZoom {
  37. [self layoutImageView];
  38. }
  39. - (void)layoutImageView {
  40. if (!self.imageView.image) return;
  41. CGSize imageSize = self.imageView.image.size;
  42. CGSize boundsSize = self.bounds.size;
  43. // 1. 计算使宽度匹配屏幕的缩放比例
  44. CGFloat widthScale = boundsSize.width / imageSize.width;
  45. // 2. 设置缩放范围
  46. self.minimumZoomScale = widthScale;
  47. self.maximumZoomScale = widthScale * 4.0;
  48. self.zoomScale = widthScale;
  49. // 3. 调整contentSize和imageView的frame
  50. [self updateContentSize];
  51. [self centerImageView];
  52. }
  53. - (void)updateContentSize {
  54. if (!self.imageView.image) return;
  55. CGSize imageSize = self.imageView.image.size;
  56. CGFloat scale = self.zoomScale;
  57. // 内容大小要基于缩放后的尺寸
  58. self.contentSize = CGSizeMake(imageSize.width * scale, imageSize.height * scale);
  59. self.imageView.frame = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
  60. }
  61. - (void)centerImageView {
  62. CGSize boundsSize = self.bounds.size;
  63. CGSize contentSize = self.contentSize;
  64. // 计算水平和垂直偏移
  65. CGFloat horizontalInset = MAX(0, (boundsSize.width - contentSize.width) / 2);
  66. CGFloat verticalInset = MAX(0, (boundsSize.height - contentSize.height) / 2);
  67. // 设置contentInset保持居中
  68. self.contentInset = UIEdgeInsetsMake(verticalInset, horizontalInset, verticalInset, horizontalInset);
  69. }
  70. #pragma mark - UIScrollViewDelegate
  71. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  72. return self.imageView;
  73. }
  74. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  75. [self centerImageView];
  76. }
  77. #pragma mark - Gesture Handlers
  78. - (void)handleDoubleTap:(UITapGestureRecognizer *)gesture {
  79. if (self.zoomScale > self.minimumZoomScale) {
  80. [self setZoomScale:self.minimumZoomScale animated:YES];
  81. } else {
  82. CGFloat newZoomScale = self.maximumZoomScale;
  83. CGPoint touchPoint = [gesture locationInView:self.imageView];
  84. CGFloat w = self.bounds.size.width / newZoomScale;
  85. CGFloat h = self.bounds.size.height / newZoomScale;
  86. [self zoomToRect:CGRectMake(touchPoint.x - w/2, touchPoint.y - h/2, w, h)
  87. animated:YES];
  88. }
  89. }
  90. - (void)handleSingleTap:(UITapGestureRecognizer *)gesture {
  91. // 通知控制器单击事件
  92. if ([self.superview respondsToSelector:@selector(handleSingleTap:)]) {
  93. [self.superview performSelector:@selector(handleSingleTap:) withObject:gesture];
  94. }
  95. }
  96. @end