JXImageScrollVC.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // JXImageScrollVC.m
  3. // shiku_im
  4. //
  5. // Created by Apple on 16/3/14.
  6. // Copyright © 2016年 Reese. All rights reserved.
  7. //
  8. #import "JXImageScrollVC.h"
  9. #import "JXImageView.h"
  10. @interface JXImageScrollVC () <UIScrollViewDelegate,UIGestureRecognizerDelegate>
  11. @end
  12. @implementation JXImageScrollVC
  13. - (instancetype)init
  14. {
  15. self = [super init];
  16. if (self) {
  17. }
  18. return self;
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = [UIColor blackColor];
  23. self.modalPresentationStyle = UIModalPresentationFullScreen;
  24. [self createScrollView];
  25. [self setScrollViewProperty];
  26. [self addTapGR];
  27. }
  28. - (void)createScrollView
  29. {
  30. self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  31. //给ScrollView添加子视图,
  32. [self.scrollView addSubview:self.iv];
  33. //设置ScrollView内容的大小
  34. self.scrollView.contentSize = self.imageSize;
  35. [self.view addSubview:self.scrollView];
  36. }
  37. //调整ScrollView的property
  38. - (void)setScrollViewProperty
  39. {
  40. self.scrollView.alwaysBounceHorizontal = YES;
  41. self.scrollView.alwaysBounceVertical = YES;
  42. //设置代理
  43. self.scrollView.delegate = self;
  44. //指定scrollView的最大缩放倍率
  45. self.scrollView.maximumZoomScale = 5.0;
  46. //指定scrollView的最小缩放倍率
  47. self.scrollView.minimumZoomScale = 1.0;
  48. //scrollView内容距离上下的边界边距
  49. self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
  50. }
  51. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  52. {
  53. return scrollView.subviews[0];
  54. }
  55. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
  56. {
  57. }
  58. - (void)scrollViewDidZoom:(UIScrollView *)scrollView
  59. {
  60. CGFloat xcenter = scrollView.center.x , ycenter = scrollView.center.y;
  61. //目前contentsize的width是否大于原scrollview的contentsize,如果大于,设置imageview中心x点为contentsize的一半,以固定imageview在该contentsize中心。如果不大于说明图像的宽还没有超出屏幕范围,可继续让中心x点为屏幕中点,此种情况确保图像在屏幕中心。
  62. xcenter = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width/2 : xcenter;
  63. ycenter = scrollView.contentSize.height > scrollView.frame.size.height ? scrollView.contentSize.height/2 : ycenter;
  64. [self.iv setCenter:CGPointMake(xcenter, ycenter)];
  65. }
  66. - (void)addTapGR
  67. {
  68. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapStart:)];
  69. tap.delegate = self;
  70. [self.scrollView addGestureRecognizer:tap];
  71. }
  72. - (void)tapStart:(UITapGestureRecognizer*)tap
  73. {
  74. // [UIView animateWithDuration:0.3 animations:^{
  75. // self.iv.frame = CGRectMake(0, 0, 0, 0);
  76. // self.iv.center = self.view.center;
  77. // self.view.alpha = 0;
  78. // } completion:^(BOOL finished) {
  79. // [self.view removeFromSuperview];
  80. [self dismissViewControllerAnimated:YES completion:nil];
  81. // }];
  82. }
  83. @end