《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRRecomPhotoView.m 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // DRRecomPhotoView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2019/1/7.
  6. // Copyright © 2019年 kuxuan. All rights reserved.
  7. //
  8. #import "DRRecomPhotoView.h"
  9. #import "SDPhotoBrowser.h"
  10. #import "UIView+SDAutoLayout.h"
  11. @interface DRRecomPhotoView ()<SDPhotoBrowserDelegate>
  12. @property (nonatomic, strong) NSArray *imageViewsArray;
  13. @end
  14. @implementation DRRecomPhotoView
  15. - (instancetype)initWithFrame:(CGRect)frame
  16. {
  17. if (self = [super initWithFrame:frame]) {
  18. [self setup];
  19. }
  20. return self;
  21. }
  22. - (void)setup
  23. {
  24. NSMutableArray *temp = [NSMutableArray new];
  25. for (int i = 0; i < 9; i++) {
  26. UIImageView *imageView = [UIImageView new];
  27. [self addSubview:imageView];
  28. imageView.userInteractionEnabled = YES;
  29. imageView.contentMode = UIViewContentModeScaleAspectFill;
  30. imageView.layer.masksToBounds = YES;
  31. imageView.tag = i;
  32. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
  33. [imageView addGestureRecognizer:tap];
  34. [temp addObject:imageView];
  35. }
  36. self.imageViewsArray = [temp copy];
  37. }
  38. - (void)setPicPathStringsArray:(NSArray *)picPathStringsArray
  39. {
  40. _picPathStringsArray = picPathStringsArray;
  41. for (long i = _picPathStringsArray.count; i < self.imageViewsArray.count; i++) {
  42. UIImageView *imageView = [self.imageViewsArray objectAtIndex:i];
  43. imageView.hidden = YES;
  44. }
  45. if (_picPathStringsArray.count == 0) {
  46. self.height = 0;
  47. self.fixedHeight = @(0);
  48. return;
  49. }
  50. CGFloat itemW = [self itemWidthForPicPathArray:_picPathStringsArray];
  51. CGFloat itemH = 0;
  52. // if (_picPathStringsArray.count == 1) {
  53. // UIImage *image = [UIImage imageNamed:_picPathStringsArray.firstObject];
  54. // if (image.size.width) {
  55. // itemH = image.size.height / image.size.width * itemW;
  56. // }
  57. // } else {
  58. // itemH = itemW;
  59. // }
  60. itemH = itemW;
  61. long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
  62. CGFloat margin = 5;
  63. [_picPathStringsArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  64. long columnIndex = idx % perRowItemCount;
  65. long rowIndex = idx / perRowItemCount;
  66. UIImageView *imageView = [_imageViewsArray objectAtIndex:idx];
  67. imageView.hidden = NO;
  68. [imageView yy_setImageWithURL:[NSURL URLWithString:obj] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
  69. imageView.backgroundColor = [UIColor clearColor];
  70. imageView.frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
  71. }];
  72. CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
  73. int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
  74. CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
  75. self.width = w;
  76. self.height = h;
  77. self.fixedHeight = @(h);
  78. self.fixedWidth = @(w);
  79. }
  80. #pragma mark - private actions
  81. - (void)tapImageView:(UITapGestureRecognizer *)tap
  82. {
  83. UIView *imageView = tap.view;
  84. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  85. browser.currentImageIndex = imageView.tag;
  86. browser.sourceImagesContainerView = self;
  87. browser.imageCount = self.picPathStringsArray.count;
  88. browser.delegate = self;
  89. [browser show];
  90. [MobClick event:OptimizingCircleMorningClick];
  91. }
  92. - (CGFloat)itemWidthForPicPathArray:(NSArray *)array
  93. {
  94. if (array.count == 1) {
  95. return 144;
  96. } else {
  97. CGFloat margin = 5;
  98. CGFloat w = ([UIScreen mainScreen].bounds.size.width-20-margin*2)/3;
  99. return w;
  100. }
  101. }
  102. - (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
  103. {
  104. // if (array.count < 3) {
  105. // return array.count;
  106. // } else if (array.count <= 4) {
  107. // return 2;
  108. // } else {
  109. // return 3;
  110. // }
  111. if (array.count<=3) {
  112. return array.count;
  113. }
  114. return 3;
  115. }
  116. #pragma mark - SDPhotoBrowserDelegate
  117. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
  118. {
  119. NSString *imageName = self.picPathStringsArray[index];
  120. NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
  121. return url;
  122. }
  123. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
  124. {
  125. UIImageView *imageView = self.subviews[index];
  126. return imageView.image;
  127. }
  128. @end