SkimViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #import "SkimViewController.h"
  2. @interface SkimViewController () <UIScrollViewDelegate>
  3. {
  4. NSArray *_imageNames;
  5. NSInteger _index;
  6. UIScrollView *_scrollView;
  7. NSMutableArray *_imageViews;
  8. }
  9. @end
  10. @implementation SkimViewController
  11. - (instancetype)initWithImageNames:(NSArray *)imageNames index:(NSInteger)index
  12. {
  13. self = [super init];
  14. if (self) {
  15. _imageNames = [[NSArray alloc] initWithArray:imageNames];
  16. _index = index;
  17. }
  18. return self;
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = [UIColor blackColor];
  23. [self goBackByNavigation];
  24. [self createScrollView];
  25. [self creaeteImageViews];
  26. }
  27. - (void)didReceiveMemoryWarning {
  28. [super didReceiveMemoryWarning];
  29. // Dispose of any resources that can be recreated.
  30. }
  31. - (void)createScrollView
  32. {
  33. CGSize size = self.view.frame.size;
  34. _scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
  35. _scrollView.delegate = self;
  36. if (@available(iOS 11.0, *)) {
  37. _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  38. }else {
  39. self.automaticallyAdjustsScrollViewInsets = NO;
  40. }
  41. _scrollView.pagingEnabled = YES;
  42. _scrollView.showsHorizontalScrollIndicator = NO;
  43. _scrollView.contentSize = CGSizeMake(size.width*3, size.height);
  44. _scrollView.contentOffset = CGPointMake(size.width, 0);
  45. [self.view addSubview:_scrollView];
  46. }
  47. - (void)creaeteImageViews
  48. {
  49. //创建用于保存图片视图的数组
  50. _imageViews = [[NSMutableArray alloc] init];
  51. CGSize size = _scrollView.frame.size;
  52. NSInteger count = _imageNames.count;
  53. for (NSInteger i=0; i<3; i++) {
  54. UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(i*size.width, 0, size.width, size.height)];
  55. view.tag = (i-1+count+_index)%count;
  56. view.contentMode = UIViewContentModeScaleAspectFit;
  57. [self setImageToView:view];
  58. if (i != 1) {
  59. //添加到滚动视图上
  60. [_scrollView addSubview:view];
  61. } else {
  62. //创建一个等大的滚动视图
  63. UIScrollView *sv = [[UIScrollView alloc] initWithFrame:view.frame];
  64. //注意 啊 大兄弟
  65. view.frame = CGRectMake(0, 0, size.width, size.height);
  66. //缩放比例
  67. sv.minimumZoomScale = 0.2;
  68. sv.maximumZoomScale = 2.0;
  69. sv.delegate = self;
  70. //手势
  71. //长按
  72. UILongPressGestureRecognizer * pressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
  73. [view addGestureRecognizer:pressGestureRecognizer];
  74. //单击
  75. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
  76. [view addGestureRecognizer:tap];
  77. view.userInteractionEnabled = YES;
  78. [sv addSubview:view];
  79. [_scrollView addSubview:sv];
  80. //双击手势
  81. UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
  82. doubleTap.numberOfTapsRequired = 2;
  83. //单机必须在双击手势识别失败时,才能识别
  84. [tap requireGestureRecognizerToFail:doubleTap];
  85. [view addGestureRecognizer:doubleTap];
  86. }
  87. //保存到数组中
  88. [_imageViews addObject:view];
  89. }
  90. //刷新标题
  91. [self refreshTitle];
  92. }
  93. - (void)tapHandle:(UITapGestureRecognizer *)tap
  94. {
  95. if (tap.numberOfTapsRequired == 1) {
  96. BOOL hidden = !self.navigationController.navigationBarHidden;
  97. [self.navigationController setNavigationBarHidden:hidden animated:YES];
  98. } else if (tap.numberOfTapsRequired == 2) {
  99. UIScrollView *sv = (UIScrollView *)[tap.view superview];
  100. if (sv.zoomScale != 1.0) {
  101. [sv setZoomScale:1.0 animated:YES];
  102. } else {
  103. [sv setZoomScale:sv.maximumZoomScale animated:YES];
  104. [self.navigationController setNavigationBarHidden:YES animated:YES];
  105. }
  106. }
  107. }
  108. - (void)setImageToView:(UIImageView *)view {
  109. [view sd_setImageWithURL:[NSURL URLWithString:_imageNames[view.tag]]];
  110. }
  111. - (void)cycleReuse
  112. {
  113. CGFloat offset = _scrollView.contentOffset.x;
  114. CGFloat width = _scrollView.frame.size.width;
  115. NSInteger flag = 0;
  116. if (offset == 0) {
  117. //向右滑
  118. flag = 1;
  119. } else if (offset == 2*width) {
  120. //向左滑
  121. flag = -1;
  122. } else {
  123. return;
  124. }
  125. NSInteger count = _imageNames.count;
  126. for (UIImageView *view in _imageViews) {
  127. view.tag = (view.tag-flag+count)%count;
  128. [self setImageToView:view];
  129. }
  130. //无动画的重置偏移量
  131. _scrollView.contentOffset = CGPointMake(width, 0);
  132. //刷新标题
  133. [self refreshTitle];
  134. //恢复中间滚动视图的缩放比例1.0
  135. UIScrollView *sv = (UIScrollView *)[_imageViews[1] superview];
  136. sv.zoomScale = 1.0;
  137. }
  138. - (void)refreshTitle
  139. {
  140. self.navigationItem.title = [NSString stringWithFormat:@"%ld/%lu",[_imageViews[1] tag]+1,_imageNames.count];
  141. }
  142. #pragma mark 长按保存图片
  143. - (void)longPressAction:(UILongPressGestureRecognizer *)pressGestureRecognizer {
  144. if (pressGestureRecognizer.state == UIGestureRecognizerStateBegan) {
  145. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存图片" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  146. [alertView show];
  147. }
  148. }
  149. //保存图片代理方法
  150. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  151. if (buttonIndex == 1) {
  152. NSURL * url = [NSURL URLWithString:_imageNames[_index]];
  153. NSURLRequest * request = [NSURLRequest requestWithURL:url];
  154. NSOperationQueue * queue = [[NSOperationQueue alloc] init];
  155. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
  156. UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:data], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  157. }];
  158. }
  159. }
  160. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  161. {
  162. if (!error) {
  163. ShowMsg(@"已保存到系统相册");
  164. }else{
  165. NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
  166. }
  167. }
  168. #pragma mark - 代理方法
  169. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  170. {
  171. [self cycleReuse];
  172. }
  173. //不用跟_scrolview区分,因为后者没有设置比例
  174. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  175. {
  176. return _imageViews[1];
  177. }
  178. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
  179. {
  180. //当缩放结束,比例小于0.5时,返回到上一页
  181. if (scale < 0.5) {
  182. //返回上一页,显示导航条
  183. if (self.navigationController.navigationBarHidden) {
  184. self.navigationController.navigationBarHidden = NO;
  185. }
  186. [self.navigationController popViewControllerAnimated:YES];
  187. }
  188. }
  189. @end