YBShowBigImageView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // YBShowBigImageView.m
  3. // YBPlaying
  4. //
  5. // Created by IOS1 on 2019/10/30.
  6. // Copyright © 2019 IOS1. All rights reserved.
  7. //
  8. #import "YBShowBigImageView.h"
  9. @interface YBShowBigImageView ()<UIScrollViewDelegate>
  10. @end
  11. @implementation YBShowBigImageView
  12. - (instancetype)initWithFrame:(CGRect)frame{
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. self.delegate = self;
  16. self.maximumZoomScale = 5.0;//最大缩放倍数
  17. self.minimumZoomScale = 1.0;//最小缩放倍数
  18. self.showsVerticalScrollIndicator = NO;
  19. self.showsHorizontalScrollIndicator = NO;
  20. self.backgroundColor = [UIColor blackColor];
  21. UIImageView *imageView = [[UIImageView alloc] init];
  22. imageView.frame = CGRectMake(0, 0, self.width, self.height);
  23. imageView.userInteractionEnabled = YES;//在UIImageView上加手势识别,打开用户交互
  24. imageView.contentMode = UIViewContentModeScaleAspectFit;
  25. UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
  26. [doubleTap setNumberOfTapsRequired:2];
  27. [imageView addGestureRecognizer:doubleTap];//添加双击手势
  28. [self addSubview:imageView];
  29. self.imageView = imageView;
  30. }
  31. return self;
  32. }
  33. - (void)setImage:(UIImage *)image{
  34. // _image = image;
  35. self.imageView.image = image;
  36. CGFloat width = image.size.width;
  37. CGFloat height = image.size.height;
  38. CGFloat maxHeight = _window_height;
  39. CGFloat maxWidth = _window_width;
  40. //如果图片尺寸大于view尺寸,按比例缩放
  41. if(width > maxWidth || height > width){
  42. CGFloat ratio = height / width;
  43. CGFloat maxRatio = maxHeight / maxWidth;
  44. if(ratio < maxRatio){
  45. width = maxWidth;
  46. height = width*ratio;
  47. }else{
  48. height = maxHeight;
  49. width = height / ratio;
  50. }
  51. }
  52. self.imageView.frame = CGRectMake((maxWidth - width) / 2, (maxHeight - height) / 2, width, height);
  53. }
  54. #pragma mark UIScrollViewDelegate
  55. //指定缩放UIScrolleView时,缩放UIImageView来适配
  56. -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
  57. return self.imageView;
  58. }
  59. //缩放后让图片显示到屏幕中间
  60. -(void)scrollViewDidZoom:(UIScrollView *)scrollView{
  61. CGSize originalSize = scrollView.bounds.size;
  62. CGSize contentSize = scrollView.contentSize;
  63. CGFloat offsetX = originalSize.width > contentSize.width ? (originalSize.width - contentSize.width) / 2 : 0;
  64. CGFloat offsetY = originalSize.height > contentSize.height ? (originalSize.height - contentSize.height) / 2 : 0;
  65. self.imageView.center = CGPointMake(contentSize.width / 2 + offsetX, contentSize.height / 2 + offsetY);
  66. }
  67. - (void)handleDoubleTap:(UITapGestureRecognizer *)recongnizer
  68. {
  69. UIGestureRecognizerState state = recongnizer.state;
  70. switch (state) {
  71. case UIGestureRecognizerStateBegan:
  72. break;
  73. case UIGestureRecognizerStateChanged:
  74. break;
  75. case UIGestureRecognizerStateCancelled:
  76. case UIGestureRecognizerStateEnded:
  77. {
  78. //以点击点为中心,放大图片
  79. CGPoint touchPoint = [recongnizer locationInView:recongnizer.view];
  80. BOOL zoomOut = self.zoomScale == self.minimumZoomScale;
  81. CGFloat scale = zoomOut?self.maximumZoomScale:self.minimumZoomScale;
  82. [UIView animateWithDuration:0.3 animations:^{
  83. self.zoomScale = scale;
  84. if(zoomOut){
  85. CGFloat x = touchPoint.x*scale - self.bounds.size.width / 2;
  86. CGFloat maxX = self.contentSize.width-self.bounds.size.width;
  87. CGFloat minX = 0;
  88. x = x > maxX ? maxX : x;
  89. x = x < minX ? minX : x;
  90. CGFloat y = touchPoint.y * scale-self.bounds.size.height / 2;
  91. CGFloat maxY = self.contentSize.height-self.bounds.size.height;
  92. CGFloat minY = 0;
  93. y = y > maxY ? maxY : y;
  94. y = y < minY ? minY : y;
  95. self.contentOffset = CGPointMake(x, y);
  96. }
  97. }];
  98. }
  99. break;
  100. default:break;
  101. }
  102. }
  103. @end