// // YOUPAIOCBarrageCoustmerCell.m // livePro // // Created by leo on 2019/11/25. // Copyright © 2019 caiPro. All rights reserved. // #import "YOUPAIOCBarrageCoustmerCell.h" #import "UIImageView+WebCache.h" @implementation YOUPAIOCBarrageCoustmerCell - (instancetype)init { self = [super init]; if (self) { self.userInteractionEnabled = YES; } return self; } - (void)prepareForReuse { [super prepareForReuse]; //因为在点击的时候被改为了红色, 所以在重用的时候, 要重置一下颜色 } - (void)setBarrageDescriptor:(YOUPAIOCBarrageDescriptor *)barrageDescriptor { [super setBarrageDescriptor:barrageDescriptor]; self.des = (YOUPAIOCBarrageTextDescriptor *)barrageDescriptor; } - (void)youpaifupdateSubviewsData { [super youpaifupdateSubviewsData]; for (UIView *subview in self.subviews) { [subview removeFromSuperview]; } self.backgroundColor = [UIColor clearColor]; self.dataModel = self.des.dataModel; UIImageView *vipImgV = [[UIImageView alloc] init]; vipImgV.contentMode = UIViewContentModeScaleAspectFit; [vipImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.dataModel.vip_icon]]; [self addSubview:vipImgV]; [vipImgV mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self); make.left.offset(0.0f); make.size.mas_offset(CGSizeMake(57.0f, 30.0f)); }]; UIView *backView = [[UIView alloc] init]; [self addSubview:backView]; [backView mas_remakeConstraints:^(MASConstraintMaker *make) { if (self.dataModel.vip_icon.length == 0) { make.left.offset(0.0f); }else{ make.left.equalTo(vipImgV.mas_centerX); } //make.centerY.equalTo(vipImgV); make.height.greaterThanOrEqualTo(30.0f); make.top.bottom.equalTo(self); }]; [self bringSubviewToFront:vipImgV]; if (self.dataModel.vip_icon.length == 0) { vipImgV.hidden = YES; }else{ vipImgV.hidden = NO; } UIImageView *avatarImgV = [[UIImageView alloc] init]; avatarImgV.contentMode = UIViewContentModeScaleAspectFit; NSString* avatarImgUrlStr = [NSString stringWithFormat:@"%@/%@",[LCSaveData getImageUrl]?[LCSaveData getImageUrl]:BaseImgUrl,self.dataModel.avatar]; [avatarImgV sd_setImageWithURL:[NSURL URLWithString:avatarImgUrlStr]]; [backView addSubview:avatarImgV]; [avatarImgV mas_remakeConstraints:^(MASConstraintMaker *make) { if (self.dataModel.vip_icon.length == 0) { make.left.offset(5.0f); }else{ make.left.equalTo(vipImgV.mas_right).offset(3.0f); } make.centerY.equalTo(backView); make.size.mas_offset(CGSizeMake(20.0f, 20.0f)); }]; [avatarImgV layoutIfNeeded]; [LCTools clipCorner:UIRectCornerAllCorners View:avatarImgV size:CGSizeMake(10.0f, 10.0f)]; UILabel *contentL = [[UILabel alloc] init]; contentL.textColor = [UIColor whiteColor]; contentL.numberOfLines = 0; contentL.font = LCFont(13.0f); contentL.textAlignment = NSTextAlignmentCenter; [backView addSubview:contentL]; NSString *textStr = [NSString stringWithFormat:@"%@ 送给 %@ %@x%@",self.dataModel.nickname,self.dataModel.tonickname,self.dataModel.giftname,@(self.dataModel.gifttotal)]; NSMutableAttributedString *attributer = [[NSMutableAttributedString alloc]initWithString:textStr]; ; [attributer addAttribute:NSForegroundColorAttributeName //value:HexColorFromRGB(0xFFDC80) value:UIColor.whiteColor range:[textStr rangeOfString:self.dataModel.nickname]]; [attributer addAttribute:NSForegroundColorAttributeName //value:HexColorFromRGB(0xFFDC80) value:UIColor.whiteColor range:[textStr rangeOfString:self.dataModel.tonickname]]; contentL.attributedText = attributer; [contentL mas_remakeConstraints:^(MASConstraintMaker *make) { // if (self.dataModel.vip_icon.length == 0) { // make.left.offset(5.0f); // }else{ // make.left.equalTo(vipImgV.mas_right).offset(3.0f); // } make.left.equalTo(avatarImgV.mas_right).offset(3.0); make.centerY.equalTo(backView); }]; UIImageView *giftImgV = [[UIImageView alloc] init]; giftImgV.contentMode = UIViewContentModeScaleAspectFit; [giftImgV setImage:[LCTools giftImgWithGiftUrlStr:self.dataModel.giftimg]]; [backView addSubview:giftImgV]; [giftImgV mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(contentL.mas_right).offset(3.0f); make.centerY.equalTo(backView); make.size.mas_offset(CGSizeMake(30.0f, 30.0f)); make.right.offset(-10.0f); }]; [self addGradientLayerWithBackView:backView]; } - (void)layoutSubviews { [super layoutSubviews]; } - (void)youpaiflayoutContentSubviews { [super youpaiflayoutContentSubviews]; } - (void)addGradientLayerWithBackView:(UIView *)backView{ if (!self.des.gradientColor) { return; } [self layoutIfNeeded]; //[LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerBottomLeft View:backView size:CGSizeMake(15.0f, 15.0f)]; [LCTools clipCorner:UIRectCornerAllCorners View:self size:CGSizeMake(15.0f, 15.0f)]; UIColor *color = HexColorFromRGB(0xC55700); color = self.des.gradientColor; CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.colors = @[ (__bridge id)(color).CGColor, (__bridge id)[(color) colorWithAlphaComponent:0.5].CGColor, (__bridge id)[(color) colorWithAlphaComponent:0.5].CGColor ]; gradientLayer.locations = @[@0.2,@0.6,@1.0]; gradientLayer.startPoint = CGPointMake(0, 0); gradientLayer.endPoint = CGPointMake(1.0, 0); gradientLayer.frame = CGRectMake(0.0, 0.0, backView.frame.size.width, backView.frame.size.height); UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:gradientLayer.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(0, 0)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = gradientLayer.bounds; maskLayer.path = maskPath.CGPath; gradientLayer.mask = maskLayer; _gradientLayer = gradientLayer; [backView.layer insertSublayer:gradientLayer atIndex:0]; } - (void)youpaifconvertContentToImage { /* UIImage *contentImage = [self.layer youpaifconvertContentToImageWithSize:CGSizeMake(CGRectGetMaxX(self.backView.frame), CGRectGetMaxY(self.backView.frame))]; [self.layer setContents:(__bridge id)contentImage.CGImage]; */ } - (void)removeSubViewsAndSublayers { //如果不要删除leftImageView, middleImageView, rightImageView, textLabel, 只需重写这个方法并留空就可以了. //比如: 你想在这个cell被点击的时候, 修改文本颜色 } - (void)youpaifaddBarrageAnimationWithDelegate:(id)animationDelegate { if (!self.superview) { return; } CGPoint startCenter = CGPointMake(CGRectGetMaxX(self.superview.bounds) + CGRectGetWidth(self.bounds)/2, self.center.y); //CGPoint stopCenter = CGPointMake((CGRectGetWidth(self.bounds)/2) + 20, self.center.y); //CGPoint endCenter = CGPointMake(-(CGRectGetWidth(self.bounds)/2), self.center.y); CGPoint stopCenter = CGPointMake(-(CGRectGetWidth(self.bounds)), self.center.y); CGPoint endCenter = CGPointMake(-(CGRectGetWidth(self.bounds) - 20), self.center.y); ///* CAKeyframeAnimation *walkAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; walkAnimation.values = @[[NSValue valueWithCGPoint:startCenter], [NSValue valueWithCGPoint:stopCenter], [NSValue valueWithCGPoint:stopCenter], [NSValue valueWithCGPoint:endCenter]]; walkAnimation.keyTimes = @[@(0.0), @(0.35), @(0.75), @(1.0)]; walkAnimation.duration = self.barrageDescriptor.animationDuration; walkAnimation.repeatCount = 1; walkAnimation.delegate = animationDelegate; walkAnimation.removedOnCompletion = NO; walkAnimation.fillMode = kCAFillModeForwards; [self.layer addAnimation:walkAnimation forKey:kBarrageAnimation]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end