YBScrollImageView.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // YBScrollImageView.m
  3. // YBHiMo
  4. //
  5. // Created by YB007 on 2021/8/20.
  6. // Copyright © 2021 YB007. All rights reserved.
  7. //
  8. #import "YBScrollImageView.h"
  9. #import "YBShowBigImageView.h"
  10. #import "SDWebImageDownloader.h"
  11. @interface YBScrollImageView ()<UIScrollViewDelegate>{
  12. NSInteger currentIndex;
  13. UILabel *indexLb;
  14. UIButton *deleteBtn;
  15. UIView *navi;
  16. BOOL isMine;
  17. }
  18. @property (nonatomic,copy) YBImageViewBlock returnBlock;
  19. @end
  20. @implementation YBScrollImageView
  21. {
  22. UITapGestureRecognizer *tap;
  23. UIScrollView *backScrollV;
  24. NSMutableArray *imageArray;
  25. NSMutableArray *imgViewArray;
  26. }
  27. - (instancetype)initWithImageArray:(NSArray *)array andIndex:(NSInteger)index andMine:(BOOL)ismine andBlock:(nonnull YBImageViewBlock)block{
  28. self = [super init];
  29. isMine = ismine;
  30. imageArray = [array mutableCopy];
  31. currentIndex = index;
  32. self.returnBlock = block;
  33. self.frame = CGRectMake(0, 0, _window_width, _window_height);
  34. if (self) {
  35. self.userInteractionEnabled = YES;
  36. tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showHideNavi)];
  37. [self addGestureRecognizer:tap];
  38. backScrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(_window_width/2, _window_height/2, 0, 0)];
  39. backScrollV.backgroundColor = [UIColor blackColor];
  40. backScrollV.contentSize = CGSizeMake(_window_width*imageArray.count, 0);
  41. backScrollV.contentOffset = CGPointMake(_window_width * index, 0);
  42. backScrollV.delegate = self;
  43. backScrollV.pagingEnabled=YES;
  44. //设置最大伸缩比例
  45. backScrollV.maximumZoomScale=1;
  46. //设置最小伸缩比例
  47. backScrollV.minimumZoomScale=1;
  48. backScrollV.showsHorizontalScrollIndicator = NO;
  49. backScrollV.showsVerticalScrollIndicator = NO;
  50. [self addSubview:backScrollV];
  51. imgViewArray = [NSMutableArray array];
  52. for (int i = 0; i < imageArray.count; i++) {
  53. id imageContent = imageArray[i];
  54. YBShowBigImageView *imgV = [[YBShowBigImageView alloc]initWithFrame:CGRectMake(_window_width*i, 0, _window_width, _window_height)];
  55. if ([imageContent isKindOfClass:[UIImage class]]) {
  56. imgV.imageView.image = imageContent;
  57. }else if ([imageContent isKindOfClass:[NSString class]]){
  58. [imgV.imageView sd_setImageWithURL:[NSURL URLWithString:imageContent]];
  59. }else if ([imageContent isKindOfClass:[NSDictionary class]]){
  60. [imgV.imageView sd_setImageWithURL:[NSURL URLWithString:strFormat([imageContent valueForKey:@"thumb"])]];
  61. }
  62. [backScrollV addSubview:imgV];
  63. [imgViewArray addObject:imgV];
  64. }
  65. [self showBigView];
  66. [self creatNavi];
  67. }
  68. return self;
  69. }
  70. - (void)showBigView{
  71. [UIView animateWithDuration:0.2 animations:^{
  72. backScrollV.frame = CGRectMake(0, 0, _window_width, _window_height);
  73. }];
  74. }
  75. - (void)doreturn{
  76. [UIView animateWithDuration:0.2 animations:^{
  77. backScrollV.frame = CGRectMake(_window_width/2, _window_height/2, 0, 0);
  78. }completion:^(BOOL finished) {
  79. if (self.returnBlock) {
  80. self.returnBlock(imageArray);
  81. }
  82. [backScrollV removeFromSuperview];
  83. backScrollV = nil;
  84. [self removeFromSuperview];
  85. }];
  86. }
  87. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  88. currentIndex = scrollView.contentOffset.x/_window_width;
  89. indexLb.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex+1,imageArray.count];
  90. }
  91. -(void)creatNavi {
  92. navi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, statusbarHeight+64)];
  93. navi.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  94. [self addSubview:navi];
  95. UIButton *retrunBtn = [UIButton buttonWithType:0];
  96. retrunBtn.frame = CGRectMake(10, 25+statusbarHeight, 30, 30);
  97. [retrunBtn setImage:[UIImage imageNamed:@"personBack"] forState:0];
  98. [retrunBtn addTarget:self action:@selector(doreturn) forControlEvents:UIControlEventTouchUpInside];
  99. [navi addSubview:retrunBtn];
  100. UILabel *titleL = [[UILabel alloc]init];
  101. titleL.frame = CGRectMake(_window_width/2-40, 25+statusbarHeight, 80, 30);
  102. titleL.textColor = [UIColor whiteColor];
  103. titleL.font = [UIFont systemFontOfSize:16];
  104. titleL.textAlignment = NSTextAlignmentCenter;
  105. titleL.text = YZMsg(@"预览");
  106. [navi addSubview:titleL];
  107. indexLb = [[UILabel alloc]init];
  108. indexLb.frame = CGRectMake(_window_width-100, 22+statusbarHeight, 80, 30);
  109. indexLb.textColor = [UIColor whiteColor];
  110. indexLb.font = [UIFont systemFontOfSize:15];
  111. indexLb.textAlignment = NSTextAlignmentRight;
  112. indexLb.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex+1,imageArray.count];
  113. indexLb.hidden = YES;
  114. [navi addSubview:indexLb];
  115. }
  116. -(void)deleteBtnClick{
  117. [self deleteSucess];
  118. }
  119. - (void)deleteSucess{
  120. [imageArray removeObjectAtIndex:currentIndex];
  121. if (imageArray.count == 0) {
  122. [self doreturn];
  123. }else{
  124. UIImageView *imgV = imgViewArray[currentIndex];
  125. [imgV removeFromSuperview];
  126. [imgViewArray removeObjectAtIndex:currentIndex];
  127. if (currentIndex == 0) {
  128. currentIndex = 0;
  129. }else{
  130. currentIndex -= 1;
  131. }
  132. indexLb.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex+1,imageArray.count];
  133. backScrollV.contentSize = CGSizeMake(_window_width*imageArray.count, 0);
  134. [backScrollV setContentOffset:CGPointMake(_window_width*currentIndex, 0)];
  135. for (int i = 0; i < imgViewArray.count; i ++) {
  136. YBShowBigImageView *imgVVVV = imgViewArray[i];
  137. imgVVVV.x = _window_width * i;
  138. }
  139. }
  140. }
  141. - (void)showHideNavi{
  142. navi.hidden = !navi.hidden;
  143. if (deleteBtn) {
  144. deleteBtn.hidden = navi.hidden;
  145. }
  146. [self doreturn];
  147. }
  148. -(void)hideDelete{
  149. deleteBtn.hidden = YES;
  150. }
  151. @end