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. cell.imageView.image = [UIImage imageWithName:imageName];
  43. cell.titleLabel.text = NSLocalizedString(modle.mTitle,nil);
  44. cell.titleLabel.textColor = _selectedIndex == indexPath.row ? [UIColor colorWithHexColorString:@"BAACFF"] : [UIColor whiteColor];
  45. }
  46. return cell ;
  47. }
  48. #pragma mark ---- UICollectionViewDelegate
  49. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  50. if (_selectedIndex == indexPath.row) {
  51. return ;
  52. }
  53. PFBeautyParam *model = _dataArray[indexPath.row];
  54. _selectedIndex = indexPath.row ;
  55. [self reloadData];
  56. if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(beautyCollectionView:didSelectedParam:)]) {
  57. [self.mDelegate beautyCollectionView:self didSelectedParam:model];
  58. }
  59. }
  60. #pragma mark ---- UICollectionViewDelegateFlowLayout
  61. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  62. return UIEdgeInsetsMake(16, 16, 6, 16) ;
  63. }
  64. @end
  65. @implementation FUBeautyCell
  66. -(instancetype)initWithFrame:(CGRect)frame {
  67. self = [super initWithFrame:frame];
  68. if (self) {
  69. self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.width)];
  70. self.imageView.layer.masksToBounds = YES ;
  71. self.imageView.layer.cornerRadius = frame.size.width / 2.0 ;
  72. [self addSubview:self.imageView];
  73. self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(-10, frame.size.width + 2, frame.size.width + 20, frame.size.height - frame.size.width - 2)];
  74. self.titleLabel.textAlignment = NSTextAlignmentCenter ;
  75. self.titleLabel.textColor = [UIColor whiteColor];
  76. self.titleLabel.font = [UIFont systemFontOfSize:10];
  77. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  78. [self addSubview:self.titleLabel ];
  79. }
  80. return self ;
  81. }
  82. @end