Keine Beschreibung

GalleryScrollView.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // GalleryScrollView.m
  3. // FirstLink
  4. //
  5. // Created by Lemon on 14/12/24.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "GalleryScrollView.h"
  9. @interface GalleryScrollView () <UIScrollViewDelegate>
  10. @property (nonatomic, strong) UIImageView *imageView;
  11. @property (nonatomic, assign) CGRect scaleOriginalRect;
  12. @property (nonatomic, assign) CGSize imageSize;
  13. @property (nonatomic, assign) CGRect initRect;
  14. @end
  15. @implementation GalleryScrollView
  16. - (id)init
  17. {
  18. self = [super init];
  19. if (self)
  20. {
  21. _index = 0;
  22. }
  23. return self;
  24. }
  25. - (id)initWithFrame:(CGRect)frame
  26. {
  27. self = [super initWithFrame:frame];
  28. if (self)
  29. {
  30. self.backgroundColor = [UIColor blackColor];
  31. self.showsHorizontalScrollIndicator = NO;
  32. self.showsVerticalScrollIndicator = NO;
  33. // self.pagingEnabled = YES;
  34. self.minimumZoomScale = 1.0;
  35. self.bouncesZoom = YES;
  36. self.delegate = self;
  37. self.userInteractionEnabled = YES;
  38. [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGalleryImage:)]];
  39. _imageView = [[UIImageView alloc] init];
  40. _imageView.clipsToBounds = YES;
  41. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  42. [self addSubview:_imageView];
  43. }
  44. return self;
  45. }
  46. - (void)setContentWithFrame:(CGRect)rect initRect:(CGRect)initRect
  47. {
  48. _imageView.frame = rect;
  49. _initRect = initRect;
  50. }
  51. - (void)setAnimationRect
  52. {
  53. _imageView.frame = _scaleOriginalRect;
  54. }
  55. - (void)resetFrame
  56. {
  57. self.zoomScale = 1.0;
  58. _imageView.frame = _initRect;
  59. }
  60. - (void)setImage:(UIImage *)image
  61. {
  62. if (image)
  63. {
  64. _imageView.image = image;
  65. _imageSize = image.size;
  66. // 判断首先缩放的值
  67. float scaleX = self.frame.size.width / _imageSize.width;
  68. float scaleY = self.frame.size.height / _imageSize.height;
  69. // 倍数小的,先到边缘
  70. if (scaleX > scaleY)
  71. {
  72. // Y方向先到边缘
  73. float imageViewWidth = _imageSize.width * scaleY;
  74. self.maximumZoomScale = self.frame.size.width / imageViewWidth;
  75. _scaleOriginalRect = (CGRect){self.frame.size.width/2 - imageViewWidth/2, 0, imageViewWidth, self.frame.size.height};
  76. }
  77. else
  78. {
  79. // X先到边缘
  80. float imageViewHeight = _imageSize.height * scaleX;
  81. self.maximumZoomScale = self.frame.size.height / imageViewHeight;
  82. _scaleOriginalRect = (CGRect){0, self.frame.size.height/2 - imageViewHeight/2, self.frame.size.width, imageViewHeight};
  83. }
  84. }
  85. }
  86. #pragma mark -
  87. #pragma mark - ScrollView Delegate
  88. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  89. {
  90. return _imageView;
  91. }
  92. - (void)scrollViewDidZoom:(UIScrollView *)scrollView
  93. {
  94. CGSize boundsSize = scrollView.bounds.size;
  95. CGRect imageFrame = _imageView.frame;
  96. CGSize contentSize = scrollView.contentSize;
  97. CGPoint centerPoint = CGPointMake(contentSize.width/2, contentSize.height/2);
  98. // center horizontally
  99. if (imageFrame.size.width <= boundsSize.width)
  100. {
  101. centerPoint.x = boundsSize.width/2;
  102. }
  103. // center vertically
  104. if (imageFrame.size.height <= boundsSize.height)
  105. {
  106. centerPoint.y = boundsSize.height/2;
  107. }
  108. _imageView.center = centerPoint;
  109. }
  110. #pragma mark -
  111. #pragma mark - Gesture
  112. - (IBAction)tapGalleryImage:(id)sender
  113. {
  114. if (self.touchCallBack)
  115. {
  116. self.touchCallBack(self, self.index);
  117. }
  118. }
  119. @end