Nav apraksta

XHZoomingImageView.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // XHZoomingImageView.m
  3. // XHImageViewer
  4. //
  5. // Created by 曾 宪华 on 14-2-17.
  6. // Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群(142557668). All rights reserved.
  7. //
  8. #import "XHZoomingImageView.h"
  9. @interface XHZoomingImageView () <UIScrollViewDelegate>
  10. @property (nonatomic, readwrite, strong) UIScrollView *scrollView;
  11. @property (nonatomic, strong) UIView *containerView;
  12. @end
  13. @implementation XHZoomingImageView
  14. - (void)_setup {
  15. self.clipsToBounds = YES;
  16. self.contentMode = UIViewContentModeScaleAspectFill;
  17. _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  18. _scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  19. _scrollView.showsHorizontalScrollIndicator = NO;
  20. _scrollView.showsVerticalScrollIndicator = NO;
  21. _scrollView.delegate = self;
  22. _containerView = [[UIView alloc] initWithFrame:self.bounds];
  23. [_scrollView addSubview:_containerView];
  24. [self addSubview:_scrollView];
  25. }
  26. - (void)awakeFromNib {
  27. [super awakeFromNib];
  28. [self _setup];
  29. }
  30. - (id)initWithFrame:(CGRect)frame
  31. {
  32. self = [super initWithFrame:frame];
  33. if (self) {
  34. // Initialization code
  35. [self _setup];
  36. }
  37. return self;
  38. }
  39. - (void)dealloc {
  40. [self.imageView removeObserver:self forKeyPath:@"image"];
  41. }
  42. #pragma mark- Properties
  43. - (UIImage *)image {
  44. return _imageView.image;
  45. }
  46. - (void)setImage:(UIImage *)image {
  47. if(self.imageView == nil){
  48. self.imageView = [UIImageView new];
  49. self.imageView.clipsToBounds = YES;
  50. }
  51. self.imageView.image = image;
  52. }
  53. - (void)setImageView:(UIImageView *)imageView {
  54. if(imageView != _imageView){
  55. [_imageView removeObserver:self forKeyPath:@"image"];
  56. [_imageView removeFromSuperview];
  57. _imageView = imageView;
  58. _imageView.frame = _imageView.bounds;
  59. [_imageView addObserver:self forKeyPath:@"image" options:0 context:nil];
  60. [_containerView addSubview:_imageView];
  61. _scrollView.zoomScale = 1;
  62. _scrollView.contentOffset = CGPointZero;
  63. _containerView.bounds = _imageView.bounds;
  64. [self resetZoomScale];
  65. _scrollView.zoomScale = _scrollView.minimumZoomScale;
  66. [self scrollViewDidZoom:_scrollView];
  67. }
  68. }
  69. - (BOOL)isViewing {
  70. return (_scrollView.zoomScale != _scrollView.minimumZoomScale);
  71. }
  72. #pragma mark- observe
  73. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  74. if(object==self.imageView){
  75. [self imageDidChange];
  76. }
  77. }
  78. - (void)imageDidChange {
  79. CGSize size = (self.imageView.image) ? self.imageView.image.size : self.bounds.size;
  80. CGFloat ratio = MIN(_scrollView.frame.size.width / size.width, _scrollView.frame.size.height / size.height);
  81. CGFloat W = ratio * size.width;
  82. CGFloat H = ratio * size.height;
  83. self.imageView.frame = CGRectMake(0, 0, W, H);
  84. _scrollView.zoomScale = 1;
  85. _scrollView.contentOffset = CGPointZero;
  86. _containerView.bounds = _imageView.bounds;
  87. [self resetZoomScale];
  88. _scrollView.zoomScale = _scrollView.minimumZoomScale;
  89. [self scrollViewDidZoom:_scrollView];
  90. }
  91. #pragma mark- Scrollview delegate
  92. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  93. return _containerView;
  94. }
  95. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  96. CGFloat Ws = _scrollView.frame.size.width - _scrollView.contentInset.left - _scrollView.contentInset.right;
  97. CGFloat Hs = _scrollView.frame.size.height - _scrollView.contentInset.top - _scrollView.contentInset.bottom;
  98. CGFloat W = _containerView.frame.size.width;
  99. CGFloat H = _containerView.frame.size.height;
  100. CGRect rct = _containerView.frame;
  101. rct.origin.x = MAX((Ws-W)/2, 0);
  102. rct.origin.y = MAX((Hs-H)/2, 0);
  103. _containerView.frame = rct;
  104. }
  105. - (void)resetZoomScale {
  106. CGFloat Rw = _scrollView.frame.size.width / self.imageView.frame.size.width;
  107. CGFloat Rh = _scrollView.frame.size.height / self.imageView.frame.size.height;
  108. CGFloat scale = 1;
  109. Rw = MAX(Rw, _imageView.image.size.width / (scale * _scrollView.frame.size.width));
  110. Rh = MAX(Rh, _imageView.image.size.height / (scale * _scrollView.frame.size.height));
  111. _scrollView.contentSize = _imageView.frame.size;
  112. _scrollView.minimumZoomScale = 1;
  113. _scrollView.maximumZoomScale = MAX(MAX(Rw, Rh), 1);
  114. }
  115. @end