PFBeautyView.m 3.9 KB

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