Files
peko-ios/YuMi/Modules/YMRoom/View/UserCard/View/SubViews/XPUserCardSkillCardView.m
2024-06-04 18:10:45 +08:00

98 lines
3.6 KiB
Objective-C

//
// XPUserCardSkillCardView.m
// xplan-ios
//
// Created by GreenLand on 2022/1/25.
//
#import "XPUserCardSkillCardView.h"
#import "XPUserCardSkillCollectionViewCell.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "YUMIMacroUitls.h"
#import "ThemeColor+UserCard.h"
@interface XPUserCardSkillCardView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation XPUserCardSkillCardView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initSubView];
[self initContraints];
}
return self;
}
- (void)initSubView {
[self addSubview:self.collectionView];
}
- (void)initContraints {
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.top.trailing.bottom.mas_equalTo(0);
}];
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPUserCardSkillCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPUserCardSkillCollectionViewCell class]) forIndexPath:indexPath];
cell.imageView.imageUrl = [self.dataArray safeObjectAtIndex1:indexPath.item];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 30;
CGFloat width = 60;
XPUserCardSkillCollectionViewCell * cell = (XPUserCardSkillCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
UIImage* image = cell.imageView.image;
if (image) {
CGFloat scale = image.size.width / (image.size.height > 0 ?image.size.height : 1) ;
return CGSizeMake(20 * scale, height);
} else {
NSURL *imgUrl = [NSURL URLWithString:[self.dataArray safeObjectAtIndex1:indexPath.item]];
UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
CGFloat scale = myImage.size.width / (myImage.size.height > 0 ? myImage.size.height : 1);
if (scale == 0) {
return CGSizeMake(60, height);
}else {
return CGSizeMake(20 * scale, height);
}
}
return CGSizeMake(width, height);
}
- (void)setDataArray:(NSArray *)dataArray {
_dataArray = [NSArray arrayWithArray:dataArray];
[self.collectionView reloadData];
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init];
layout.itemSize = CGSizeMake(60, 30);
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 8;
layout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor clearColor];
[_collectionView registerClass:[XPUserCardSkillCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPUserCardSkillCollectionViewCell class])];
}
return _collectionView;
}
@end