Files
peko-ios/YuMi/Modules/YMNewHome/View/Cell/XPNewHomeRecommendTableViewCell.m
2023-08-16 18:40:56 +08:00

258 lines
8.4 KiB
Objective-C

//
// XPNewHomeRecommendTableViewCell.m
// YuMi
//
// Created by YuMi on 2022/10/8.
//
#import "XPNewHomeRecommendTableViewCell.h"
///Third
#import <Masonry/Masonry.h>
#import <GKCycleScrollView/GKCycleScrollView.h>
///Tool
#import "NetImageView.h"
#import "YUMIMacroUitls.h"
#import "DJDKMIMOMColor.h"
#import "NSArray+Safe.h"
///Model
#import "HomeRecommendRoomModel.h"
@interface XPCycleScrollViewCell : GKCycleScrollViewCell
///显示头像
@property (nonatomic,strong) NetImageView *avatarImageView;
///显示tag背景
@property (nonatomic,strong) NetImageView *tagImageView;
///显示tag
@property (nonatomic,strong) UILabel *tagTitleView;
///显示标题
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,assign) BOOL isCenterView;
@property (nonatomic,strong) HomeRecommendRoomModel *recommendRoomInfo;
@end
@implementation XPCycleScrollViewCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.coverView.hidden = YES;
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.avatarImageView];
[self addSubview:self.tagImageView];
[self.tagImageView addSubview:self.tagTitleView];
[self addSubview:self.titleLabel];
}
- (void)initSubViewConstraints {
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self);
make.top.mas_equalTo(10 * kScreenScale);
make.width.height.mas_equalTo(123 * kScreenScale);
}];
[self.tagImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(18 * kScreenScale);
make.width.mas_greaterThanOrEqualTo(10);
make.left.top.mas_equalTo(self.avatarImageView);
}];
[self.tagTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(7 * kScreenScale);
make.right.mas_equalTo(-7 * kScreenScale);
make.top.bottom.equalTo(self.tagImageView);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.avatarImageView);
make.top.mas_equalTo(self.avatarImageView.mas_bottom).offset(10 * kScreenScale);
}];
}
- (void)setIsCenterView:(BOOL)isCenterView{
_isCenterView = isCenterView;
if(_isCenterView == YES){
[self.avatarImageView.superview layoutIfNeeded];
[UIView animateWithDuration:0.5 animations:^{
[self.avatarImageView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(123 * kScreenScale);
make.top.mas_equalTo(10 * kScreenScale);
}];
self.avatarImageView.layer.cornerRadius = 11;
self.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
[self.avatarImageView.superview layoutIfNeeded];
}];
return;
}
[self.avatarImageView.superview layoutIfNeeded];
[UIView animateWithDuration:0.5 animations:^{
self.avatarImageView.layer.cornerRadius = 8;
[self.avatarImageView mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(100 * kScreenScale);
make.top.mas_equalTo(22 * kScreenScale);
}];
self.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
[self.avatarImageView.superview layoutIfNeeded];
}];
}
#pragma mark - Getters And Setters
- (void)setRecommendRoomInfo:(HomeRecommendRoomModel *)recommendRoomInfo {
_recommendRoomInfo = recommendRoomInfo;
if (_recommendRoomInfo) {
self.avatarImageView.imageUrl = _recommendRoomInfo.avatar;
self.titleLabel.text = _recommendRoomInfo.title;
self.tagTitleView.hidden = YES;
self.tagImageView.hidden = YES;
if(_recommendRoomInfo.iconContent.length > 0){
self.tagTitleView.text = _recommendRoomInfo.iconContent;
self.tagTitleView.hidden = NO;
self.tagImageView.hidden = NO;
}
}
}
- (NetImageView *)avatarImageView {
if (!_avatarImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeMonentsPhoto;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
_avatarImageView.layer.masksToBounds = YES;
_avatarImageView.layer.cornerRadius = 11;
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
}
return _avatarImageView;
}
-(NetImageView *)tagImageView{
if (!_tagImageView){
_tagImageView = [[NetImageView alloc]init];
_tagImageView.image = [UIImage imageNamed:@"monents_common_room_tag"];
}
return _tagImageView;
}
- (UILabel *)tagTitleView{
if (!_tagTitleView){
_tagTitleView = [UILabel new];
_tagTitleView.font = [UIFont systemFontOfSize:11 weight:UIFontWeightMedium];
_tagTitleView.textColor = [UIColor whiteColor];
}
return _tagTitleView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
_titleLabel.textColor = [DJDKMIMOMColor mainTextColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
@end
@interface XPNewHomeRecommendTableViewCell ()<GKCycleScrollViewDelegate, GKCycleScrollViewDataSource>
@property (nonatomic,strong) GKCycleScrollView *pi_scrollView;
@property (nonatomic,strong) NSArray *recommends;
@end
@implementation XPNewHomeRecommendTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.backgroundColor = [UIColor clearColor];
self.pi_scrollView = [[GKCycleScrollView alloc] initWithFrame:CGRectZero];
self.pi_scrollView.delegate = self;
self.pi_scrollView.leftRightMargin = 0.0f;
self.pi_scrollView.topBottomMargin = 0.0f;
self.pi_scrollView.dataSource = self;
[self.contentView addSubview:self.pi_scrollView];
}
- (void)initSubViewConstraints {
[self.pi_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.contentView).inset(15 * kScreenScale);
make.top.mas_equalTo(self.contentView);
make.bottom.mas_equalTo(self.contentView);
}];
}
- (NSInteger)numberOfCellsInCycleScrollView:(GKCycleScrollView *)cycleScrollView {
return self.recommends.count;
}
/// 返回继承自GKCycleScrollViewCell的类
/// @param cycleScrollView cycleScrollView description
/// @param index 索引
- (GKCycleScrollViewCell *)cycleScrollView:(GKCycleScrollView *)cycleScrollView cellForViewAtIndex:(NSInteger)index {
XPCycleScrollViewCell * cell = cycleScrollView.dequeueReusableCell;
if (!cell) {
cell = [[XPCycleScrollViewCell alloc] init];
}
HomeRecommendRoomModel * info = [self.recommends safeObjectAtIndex1:index];
cell.recommendRoomInfo = info;
cell.isCenterView = index == self.pi_scrollView.currentSelectIndex;
return cell;
}
- (void)cycleScrollView:(GKCycleScrollView *)cycleScrollView didScrollCellToIndex:(NSInteger)index{
HomeRecommendRoomModel * info = [self.recommends safeObjectAtIndex1:index];
NSLog(@"%@",info.title);
UIView *view = cycleScrollView.subviews[0];
for (XPCycleScrollViewCell * cell in view.subviews) {
cell.isCenterView = [cell.recommendRoomInfo.title isEqual:info.title];
}
}
- (void)cycleScrollView:(GKCycleScrollView *)cycleScrollView didSelectCellAtIndex:(NSInteger)index {
if(self.recommends.count > 0 && index < self.recommends.count) {
HomeRecommendRoomModel * info = [self.recommends safeObjectAtIndex1:index];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPNewHomeRecommendTableViewCell:didSelectItem:)]) {
[self.delegate xPNewHomeRecommendTableViewCell:self didSelectItem:info];
}
}
}
- (CGSize)sizeForCellInCycleScrollView:(GKCycleScrollView *)cycleScrollView {
return CGSizeMake(123 * kScreenScale, 160 * kScreenScale);
}
#pragma mark - Getters And Setters
- (void)setRecommendsList:(NSArray *)recommends{
if(recommends.count == self.recommends.count){
return;
}
self.recommends = recommends;
[self.pi_scrollView reloadData];
}
@end