FUBeautyView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // FUBeautyView.m
  3. // FUAPIDemoBar
  4. //
  5. // Created by L on 2018/6/27.
  6. // Copyright © 2018年 L. All rights reserved.
  7. //
  8. #import "FUBeautyView.h"
  9. #import "UIImage+demobar.h"
  10. #import "UIColor+FUAPIDemoBar.h"
  11. @interface FUBeautyView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  12. @end
  13. @implementation FUBeautyView
  14. -(void)awakeFromNib {
  15. [super awakeFromNib];
  16. self.backgroundColor = [UIColor clearColor];
  17. self.delegate = self ;
  18. self.dataSource = self ;
  19. [self registerClass:[FUBeautyCell class] forCellWithReuseIdentifier:@"FUBeautyCell"];
  20. _selectedIndex = -1;
  21. }
  22. #pragma mark ---- UICollectionViewDataSource
  23. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  24. return self.dataArray.count ;
  25. }
  26. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  27. FUBeautyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FUBeautyCell" forIndexPath:indexPath];
  28. if (indexPath.row < self.dataArray.count){
  29. FUBeautyParam *modle = self.dataArray[indexPath.row] ;
  30. NSString *imageName ;
  31. BOOL opened = YES;
  32. if (modle.iSStyle101) {
  33. opened = fabs(modle.mValue - 0.5) > 0.01 ? YES : NO;
  34. }else{
  35. opened = fabsf(modle.mValue - 0) > 0.01 ? YES : NO;
  36. }
  37. BOOL selected = _selectedIndex == indexPath.row ;
  38. if (selected) {
  39. imageName = opened ? [modle.mTitle stringByAppendingString:@"-3.png"] : [modle.mTitle stringByAppendingString:@"-2.png"] ;
  40. }else {
  41. imageName = opened ? [modle.mTitle stringByAppendingString:@"-1.png"] : [modle.mTitle stringByAppendingString:@"-0.png"] ;
  42. }
  43. /* 英文icon 未找到 */
  44. UIImage *imageIcon = [UIImage imageWithName:imageName];
  45. if (imageIcon == nil) {
  46. if (selected) {
  47. imageName = opened ? [modle.mTitle stringByAppendingString:@"-3.png"] : [modle.mTitle stringByAppendingString:@"-2.png"] ;
  48. }else {
  49. imageName = opened ? [modle.mTitle stringByAppendingString:@"-1.png"] : [modle.mTitle stringByAppendingString:@"-0.png"] ;
  50. }
  51. imageIcon = [UIImage imageWithName:imageName];
  52. }
  53. cell.imageView.image = imageIcon;
  54. cell.titleLabel.text = modle.mTitle;
  55. cell.titleLabel.textColor = _selectedIndex == indexPath.row ? [UIColor colorWithHexColorString:@"5EC7FE"] : [UIColor whiteColor];
  56. }
  57. return cell ;
  58. }
  59. #pragma mark ---- UICollectionViewDelegate
  60. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  61. if (_selectedIndex == indexPath.row) {
  62. return ;
  63. }
  64. FUBeautyParam *model = _dataArray[indexPath.row];
  65. _selectedIndex = indexPath.row ;
  66. [self reloadData];
  67. if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(beautyCollectionView:didSelectedParam:)]) {
  68. [self.mDelegate beautyCollectionView:self didSelectedParam:model];
  69. }
  70. }
  71. #pragma mark ---- UICollectionViewDelegateFlowLayout
  72. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  73. return UIEdgeInsetsMake(16, 16, 6, 16) ;
  74. }
  75. @end
  76. @implementation FUBeautyCell
  77. -(instancetype)initWithFrame:(CGRect)frame {
  78. self = [super initWithFrame:frame];
  79. if (self) {
  80. self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.width)];
  81. self.imageView.layer.masksToBounds = YES ;
  82. self.imageView.layer.cornerRadius = frame.size.width / 2.0 ;
  83. [self addSubview:self.imageView];
  84. self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(-10, frame.size.width + 2, frame.size.width + 20, frame.size.height - frame.size.width - 2)];
  85. self.titleLabel.textAlignment = NSTextAlignmentCenter ;
  86. self.titleLabel.textColor = [UIColor whiteColor];
  87. self.titleLabel.font = [UIFont systemFontOfSize:10];
  88. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  89. [self addSubview:self.titleLabel ];
  90. }
  91. return self ;
  92. }
  93. @end