// // XPHomeHotRoomCollectionViewCell.m // xplan-ios // // Created by 冯硕 on 2022/2/21. // #import "XPHomeHotRoomCollectionViewCell.h" ///Third #import #import ///Tool #import "NetImageView.h" #import "ThemeColor.h" #import "XPMacro.h" #import "UIImage+Utils.h" ///Model #import "HomeRecommendRoomModel.h" #define KRecommendRoomAvatarWidth 55 * kScreenScale @interface XPHomeHotRoomCollectionViewCell () ///背景 @property (nonatomic,strong) UIImageView *backImageView; ///房间的标签 @property (nonatomic,strong) NetImageView *roomTagImageView; ///房间的话题 @property (nonatomic,strong) UILabel *roomDesLabel; ///note @property (nonatomic,strong) UIImageView *notImageView; ///房间标题 @property (nonatomic,strong) UILabel *roomTitleLabel; ///头像 @property (nonatomic,strong) NetImageView *avatarImageView; ///性别 @property (nonatomic,strong) UIImageView *sexImageView; ///昵称 @property (nonatomic,strong) UILabel *nickLabel; ///水波纹动画 @property (nonatomic,strong) UIView *rippleView; ///动画管理类 @property (strong, nonatomic) SVGAParser *parser; ///PK中动图 @property (nonatomic, strong) SVGAImageView *svgDisplayView; @end @implementation XPHomeHotRoomCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { [self.contentView addSubview:self.backImageView]; [self.backImageView addSubview:self.rippleView]; [self.backImageView addSubview:self.roomTagImageView]; [self.backImageView addSubview:self.svgDisplayView]; [self.backImageView addSubview:self.roomDesLabel]; [self.backImageView addSubview:self.notImageView]; [self.backImageView addSubview:self.roomTitleLabel]; [self.backImageView addSubview:self.avatarImageView]; [self.backImageView addSubview:self.sexImageView]; [self.backImageView addSubview:self.nickLabel]; [self addAnimation]; } - (void)addAnimation { NSInteger pulsingCount = 3; double animationDuration = 3; CALayer * animationLayer = [CALayer layer]; for (int i = 0; i < pulsingCount; i++) { CALayer * pulsingLayer = [CALayer layer]; pulsingLayer.frame = CGRectMake(0, 0, KRecommendRoomAvatarWidth, KRecommendRoomAvatarWidth); pulsingLayer.borderColor = [UIColor whiteColor].CGColor; pulsingLayer.borderWidth = 1; pulsingLayer.cornerRadius = KRecommendRoomAvatarWidth / 2; CAMediaTimingFunction * defaultCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]; CAAnimationGroup * animationGroup = [CAAnimationGroup animation]; animationGroup.fillMode = kCAFillModeBackwards; animationGroup.beginTime = CACurrentMediaTime() + (double)i * animationDuration / (double)pulsingCount; animationGroup.duration = animationDuration; animationGroup.repeatCount = HUGE; animationGroup.removedOnCompletion = NO; animationGroup.timingFunction = defaultCurve; CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; scaleAnimation.fromValue = @1.4; scaleAnimation.toValue = @2; scaleAnimation.removedOnCompletion = NO; CAKeyframeAnimation * opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; opacityAnimation.values = @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0]; opacityAnimation.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1]; opacityAnimation.removedOnCompletion = NO; animationGroup.animations = @[scaleAnimation, opacityAnimation]; [pulsingLayer addAnimation:animationGroup forKey:@"plulsing"]; [animationLayer addSublayer:pulsingLayer]; } [self.rippleView.layer addSublayer:animationLayer]; } - (void)initSubViewConstraints { [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.contentView); }]; [self.roomTagImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(38 * kScreenScale, 18 * kScreenScale)); make.left.top.mas_equalTo(self.backImageView); }]; [self.svgDisplayView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.mas_equalTo(self.contentView); make.size.mas_equalTo(CGSizeMake(40, 20)); }]; [self.roomDesLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.backImageView).offset(13 * kScreenScale); make.right.mas_equalTo(self.backImageView).offset(-4 *kScreenScale); make.top.mas_equalTo(self.backImageView).offset(22 * kScreenScale); }]; [self.notImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(13 * kScreenScale, 9.6 * kScreenScale)); make.top.mas_equalTo(self.roomDesLabel.mas_bottom).offset(3 * kScreenScale); make.left.mas_equalTo(self.roomDesLabel); }]; [self.roomTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.notImageView.mas_right).offset(5 * kScreenScale); make.centerY.mas_equalTo(self.notImageView); make.right.mas_lessThanOrEqualTo(self.backImageView); }]; [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(KRecommendRoomAvatarWidth, KRecommendRoomAvatarWidth)); make.centerX.mas_equalTo(self.backImageView); make.bottom.mas_equalTo(self.nickLabel.mas_top).offset(-2 * kScreenScale); }]; [self.rippleView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.mas_equalTo(self.avatarImageView); make.size.mas_equalTo(CGSizeMake(KRecommendRoomAvatarWidth, KRecommendRoomAvatarWidth)); }]; [self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.backImageView).inset(5 * kScreenScale); make.bottom.mas_equalTo(self.backImageView.mas_bottom).offset(-15 * kScreenScale); }]; [self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.bottom.mas_equalTo(self.avatarImageView); make.size.mas_equalTo(CGSizeMake(14 * kScreenScale, 14 * kScreenScale)); }]; } #pragma mark - Getters And Setters - (void)setHotRoomInfo:(HomeRecommendRoomModel *)hotRoomInfo { _hotRoomInfo = hotRoomInfo; if (_hotRoomInfo) { self.avatarImageView.imageUrl = _hotRoomInfo.avatar; if (_hotRoomInfo.title.length > 6) { _hotRoomInfo.title = [_hotRoomInfo.title substringToIndex:6]; } self.roomTitleLabel.text = _hotRoomInfo.title; self.roomTagImageView.imageUrl = _hotRoomInfo.tagPict; if (_hotRoomInfo.nick.length > 7) { _hotRoomInfo.nick = [_hotRoomInfo.nick substringToIndex:7]; } self.nickLabel.text = _hotRoomInfo.nick; self.sexImageView.image = _hotRoomInfo.gender == GenderType_Female ? [UIImage imageNamed:@"common_female"] : [UIImage imageNamed:@"common_male"]; self.roomDesLabel.text = _hotRoomInfo.roomDesc; if (hotRoomInfo.crossPking) { self.roomTagImageView.hidden = YES; self.svgDisplayView.hidden = NO; [self.parser parseWithNamed:@"anchorPk_crossPking" inBundle:[NSBundle mainBundle] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) { self.svgDisplayView.loops = INT_MAX; self.svgDisplayView.clearsAfterStop = NO; self.svgDisplayView.videoItem = videoItem; [self.svgDisplayView startAnimation]; } failureBlock:^(NSError * _Nonnull error) { }]; } else { self.roomTagImageView.hidden = NO; [self.svgDisplayView stopAnimation]; self.svgDisplayView.hidden = YES; } } } - (void)setIndexPathRow:(NSInteger)indexPathRow { _indexPathRow = (indexPathRow + 1); if (_indexPathRow % 2 != 0) { self.backImageView.image = [UIImage gradientColorImageFromColors:@[UIColorFromRGB(0xFADBF6), UIColorFromRGB(0xD6EFFE)] gradientType:GradientTypeTopToBottom imgSize:CGSizeMake(10, 10)]; } else { self.backImageView.image = [UIImage gradientColorImageFromColors:@[UIColorFromRGB(0xFFF8D9), UIColorFromRGB(0xEAFFD8)] gradientType:GradientTypeTopToBottom imgSize:CGSizeMake(10, 10)]; } } - (UIImageView *)backImageView { if (!_backImageView) { _backImageView = [[UIImageView alloc] init]; _backImageView.contentMode = UIViewContentModeScaleAspectFill; _backImageView.layer.masksToBounds = YES; _backImageView.layer.cornerRadius = 8; } return _backImageView; } - (NetImageView *)avatarImageView { if (!_avatarImageView) { NetImageConfig * config = [[NetImageConfig alloc]init]; config.imageType = ImageTypeUserIcon; config.placeHolder = [UIImageConstant defaultAvatarPlaceholder]; _avatarImageView = [[NetImageView alloc] initWithConfig:config]; _avatarImageView.layer.masksToBounds = YES; _avatarImageView.layer.cornerRadius = KRecommendRoomAvatarWidth/2; _avatarImageView.layer.borderColor = [UIColor whiteColor].CGColor; _avatarImageView.layer.borderWidth = 1.1; } return _avatarImageView; } - (UILabel *)roomTitleLabel { if (!_roomTitleLabel) { _roomTitleLabel = [[UILabel alloc] init]; _roomTitleLabel.font = [UIFont systemFontOfSize:10]; _roomTitleLabel.textColor = [ThemeColor textThirdColor]; } return _roomTitleLabel; } - (UILabel *)nickLabel { if (!_nickLabel) { _nickLabel = [[UILabel alloc] init]; _nickLabel.font = [UIFont systemFontOfSize:10]; _nickLabel.textColor = [ThemeColor mainTextColor]; _nickLabel.textAlignment = NSTextAlignmentCenter; } return _nickLabel; } - (UIImageView *)sexImageView { if (!_sexImageView) { _sexImageView = [[UIImageView alloc] init]; _sexImageView.userInteractionEnabled = YES; } return _sexImageView; } - (NetImageView *)roomTagImageView { if (!_roomTagImageView) { _roomTagImageView = [[NetImageView alloc] init]; _roomTagImageView.userInteractionEnabled = YES; } return _roomTagImageView; } - (UILabel *)roomDesLabel { if (!_roomDesLabel) { _roomDesLabel = [[UILabel alloc] init]; _roomDesLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium]; _roomDesLabel.textColor = UIColorFromRGB(0x000000); _roomDesLabel.numberOfLines = 0.2; } return _roomDesLabel; } - (UIImageView *)notImageView { if (!_notImageView) { _notImageView = [[UIImageView alloc] init]; _notImageView.image = [UIImage imageNamed:@"home_recommend_hot_note"]; } return _notImageView; } - (UIView *)rippleView { if (!_rippleView) { _rippleView = [[UIView alloc] init]; _rippleView.backgroundColor = [UIColor clearColor]; } return _rippleView; } - (SVGAImageView *)svgDisplayView { if (_svgDisplayView == nil) { _svgDisplayView = [[SVGAImageView alloc]init]; _svgDisplayView.contentMode = UIViewContentModeScaleToFill; _svgDisplayView.userInteractionEnabled = NO; _svgDisplayView.backgroundColor = [UIColor clearColor]; } return _svgDisplayView; } - (SVGAParser *)parser { if (!_parser) { _parser = [[SVGAParser alloc]init]; } return _parser; } @end