// // KBPopShareGoodView.m // YouHuiProject // // Created by 小花 on 2018/7/10. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBPopShareGoodView.h" #define FitYHShareGoodsWidth(X) (X) #define FitYHShareGoodsHeight(X) (X) //#define FitYHShareGoodsWidth(X) (X) //#define FitYHShareGoodsHeight(X) (X) #define QRWidth (85) @interface KBPopShareGoodView() { NSInteger count; } //主图 @property (strong, nonatomic) UIImageView *mainImageView; //商品标题 @property (strong, nonatomic) UILabel *titleLabel; //商品券后价 @property (strong, nonatomic) UILabel *ticketAfterPrice; //券钱 @property (strong, nonatomic) UILabel *ticketPrice; //券钱 @property (strong, nonatomic) UIImageView *ticketPriceBackImageView; //原始价格 @property (strong, nonatomic) UILabel *originalPrice; //二维码图片 @property (strong, nonatomic) UIImageView *QRcodeImageView; @property (strong, nonatomic) UIImageView *appImageView; @property (strong,nonatomic) UILabel *appTitle; @property (nonatomic, strong) UILabel *quanType; @property (nonatomic, strong) UILabel *commissionLabel; @property (nonatomic, strong) UIActivityIndicatorView *indicatorView; @property (nonatomic, strong) UIImageView *userIcon; @property (nonatomic, strong) UILabel *nickName; @property (nonatomic, strong) UILabel *inviteCode; @end @implementation KBPopShareGoodView -(instancetype)initWithFrame:(CGRect)frame{ if (self=[super initWithFrame:frame]) { } return self; } -(void)setModel:(KBShareGoodsModel *)model{ _model=model; [self addUI]; [self adjustUI]; [self otherOP]; } -(void)drawRect:(CGRect)rect{ [super drawRect:rect]; } -(void)addUI{ [self addSubview:self.titleLabel]; [self addSubview:self.mainImageView]; [self addSubview:self.ticketAfterPrice]; [self addSubview:self.originalPrice]; [self addSubview:self.ticketPriceBackImageView]; [self.ticketPriceBackImageView addSubview:self.ticketPrice]; [self.ticketPriceBackImageView addSubview:self.quanType]; [self addSubview:self.QRcodeImageView]; [self addSubview:self.appImageView]; [self addSubview:self.appTitle]; [self addSubview:self.commissionLabel]; [self addSubview:self.indicatorView]; [self.indicatorView startAnimating]; [self addSubview:self.userIcon]; [self addSubview:self.nickName]; [self addSubview:self.inviteCode]; if (self.model.shareGoodsPurchaseType==YHShareGoodsPurchaseTypePrice) { self.ticketPrice.hidden=YES; self.ticketPriceBackImageView.hidden=YES; }else{ self.ticketPrice.hidden=NO; self.ticketPriceBackImageView.hidden=NO; } } -(void)adjustUI{ [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(20))); make.top.mas_equalTo(FitYHShareGoodsHeight(26)); make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-20))); }]; [self.mainImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(20))); make.top.equalTo(self.titleLabel.mas_bottom).offset(FitYHShareGoodsHeight(22)); make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-20))); make.height.mas_equalTo(FitYHShareGoodsWidth(self.width-2*FITSIZE(20))); }]; [self.ticketAfterPrice mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(25))); make.top.equalTo(self.mainImageView.mas_bottom).offset(FitYHShareGoodsHeight(40)); }]; [self.originalPrice mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.ticketAfterPrice.mas_left); make.top.mas_equalTo(self.ticketAfterPrice.mas_bottom).mas_offset(FitYHShareGoodsHeight(20)); // make.width.mas_equalTo(FITSIZE(74)); }]; [self.ticketPriceBackImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.originalPrice.mas_left); make.top.mas_equalTo(self.originalPrice.mas_bottom).mas_offset(Fitsize(10)); make.width.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(64))); make.height.mas_equalTo(FitYHShareGoodsHeight(FITSIZE(14))); }]; [self.quanType mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.bottom.mas_equalTo(0); make.width.mas_equalTo(Fitsize(20)); }]; [self.ticketPrice mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(Fitsize(20)); make.right.top.bottom.mas_equalTo(0); }]; [self.QRcodeImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-19))); make.top.equalTo(self.mainImageView.mas_bottom).offset(FitYHShareGoodsHeight(20)); make.width.height.mas_equalTo(FITSIZE(QRWidth)); }]; [self.appImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.QRcodeImageView.mas_centerX); make.centerY.mas_equalTo(self.QRcodeImageView.mas_centerY); make.width.height.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(18))); }]; // [self.appTitle mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(self.appImageView.mas_top); // make.right.equalTo(self.QRcodeImageView.mas_right); // }]; [self.commissionLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.QRcodeImageView.mas_centerX); make.top.mas_equalTo(self.QRcodeImageView.mas_bottom).mas_offset(5); }]; [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.ticketAfterPrice.mas_left); make.top.mas_equalTo(self.ticketAfterPrice.mas_bottom).mas_offset(Fitsize(10)); make.width.height.mas_equalTo(Fitsize(30)); }]; [self.nickName mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.userIcon.mas_right).mas_offset(5); make.centerY.mas_equalTo(self.userIcon.mas_centerY); make.width.mas_equalTo(Fitsize(100)); make.height.mas_equalTo(Fitsize(20)); }]; [self.inviteCode mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.userIcon.mas_left); make.top.mas_equalTo(self.userIcon.mas_bottom).mas_equalTo(Fitsize(10)); make.width.mas_equalTo(Fitsize(140)); make.height.mas_equalTo(Fitsize(20)); }]; self.ticketPriceBackImageView.hidden = YES; } -(void)otherOP{ self.backgroundColor=[UIColor yhGrayColor]; [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.model.mainImageUrl] placeholderImage:[UIImage imageNamed:@"share_goods_default_img"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { count++; if (count>1) { if (self.imgSuccBlock) { self.imgSuccBlock(); } } [self.indicatorView stopAnimating]; }]; } #pragma mark 懒加载 -(UILabel *)titleLabel{ if (!_titleLabel) { _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(20)), FitYHShareGoodsHeight(46), FitYHShareGoodsWidth(self.width-2*FITSIZE(20)), 0)]; _titleLabel.font = [UIFont systemFontOfSize:14]; NSTextAttachment *textAttach = [[NSTextAttachment alloc]init]; UIImage *img; if (self.model.shareGoodsFromType==YHShareGoodsFromTypeTianMao) { img= [UIImage imageNamed:@"tm_shop"]; }else if(self.model.shareGoodsFromType==YHShareGoodsFromTypeTaoBao){ } if (img) { textAttach.image = img; } textAttach.bounds = CGRectMake(0, -3, img.size.width*0.9, img.size.height*0.9); NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",self.model.title]]; NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:textAttach]; [attri insertAttributedString:string atIndex:0]; _titleLabel.attributedText = attri; // _titleLabel.text=@"虹小型电风扇迷你手持电扇USB学生宿舍 "; _titleLabel.numberOfLines=2; [_titleLabel sizeToFit]; } return _titleLabel; } -(UIImageView *)mainImageView{ if (!_mainImageView) { _mainImageView=[[UIImageView alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(20)), self.titleLabel.bottom+FitYHShareGoodsHeight(42), FitYHShareGoodsWidth(self.width-2*FITSIZE(20)), FitYHShareGoodsWidth((self.width-2*FITSIZE(20))))]; } return _mainImageView; } -(UILabel *)ticketAfterPrice{ if (!_ticketAfterPrice) { _ticketAfterPrice=[[UILabel alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(15)), self.mainImageView.bottom+FitYHShareGoodsHeight(60), 0, 0)]; if (self.model.shareGoodsPurchaseType==YHShareGoodsPurchaseTypeCoupons) { _ticketAfterPrice.text=[NSString stringWithFormat:@"券后¥%@",self.model.ticketAfterPrice]; }else{ _ticketAfterPrice.text=[NSString stringWithFormat:@"折后¥%@",self.model.ticketAfterPrice]; } _ticketAfterPrice.font=[UIFont systemFontOfSize:14.0f]; _ticketAfterPrice.textColor=[UIColor YHColorWithHex:0xEE1515]; [_ticketAfterPrice sizeToFit]; } return _ticketAfterPrice; } -(UILabel *)originalPrice{ if (!_originalPrice) { _originalPrice=[[UILabel alloc]initWithFrame:CGRectMake(self.ticketAfterPrice.left, self.ticketAfterPrice.bottom+FitYHShareGoodsHeight(14), 0, 0)]; NSString *price=[NSString stringWithFormat:@"原价¥%@",self.model.originalPrice]; NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:price]; [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, price.length)]; [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor YHColorWithHex:0x999999] range:NSMakeRange(0, price.length)]; [_originalPrice setAttributedText:attri]; _originalPrice.textColor=[UIColor YHColorWithHex:0x999999]; _originalPrice.font=[UIFont systemFontOfSize:13.0f]; [_originalPrice sizeToFit]; // _originalPrice.centerY = self.ticketAfterPrice.centerY; } return _originalPrice; } -(UIImageView *)ticketPriceBackImageView{ if (!_ticketPriceBackImageView) { _ticketPriceBackImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.originalPrice.x, self.originalPrice.bottom+FitYHShareGoodsHeight(22), FitYHShareGoodsWidth(FITSIZE(64)), FitYHShareGoodsHeight(14))]; // _ticketPriceBackImageView.backgroundColor=[UIColor blueColor]; [_ticketPriceBackImageView setImage:[UIImage imageNamed:@"quan_detail"]]; } return _ticketPriceBackImageView; } -(UILabel *)ticketPrice{ if (!_ticketPrice) { _ticketPrice=[[UILabel alloc]init]; _ticketPrice.textColor=[UIColor whiteColor]; _ticketPrice.text=[NSString stringWithFormat:@"%@元",self.model.ticketPrice]; _ticketPrice.font=[UIFont systemFontOfSize:Fitsize(10)]; _ticketPrice.textAlignment=NSTextAlignmentCenter; } return _ticketPrice; } - (UILabel *)quanType { if (!_quanType) { _quanType = [[UILabel alloc] init]; if (self.model.shareGoodsPurchaseType==YHShareGoodsPurchaseTypeCoupons) { _quanType.text=@"券"; }else{ _quanType.text=@"折"; } _quanType.textColor=[UIColor whiteColor]; _quanType.font=[UIFont systemFontOfSize:Fitsize(10)]; _quanType.textAlignment=NSTextAlignmentCenter; } return _quanType; } - (UILabel *)commissionLabel { if (!_commissionLabel) { _commissionLabel = [[UILabel alloc] init]; _commissionLabel.text = [NSString stringWithFormat:@"购买可赚¥%@",self.model.commissionPrice]; _commissionLabel.textColor = [UIColor homeRedColor]; _commissionLabel.font = [UIFont boldSystemFontOfSize:Fitsize(12)]; _commissionLabel.text = [NSString stringWithFormat:@"扫我赚¥%@",self.model.commissionPrice]; _commissionLabel.hidden = YES; } return _commissionLabel; } -(UIImageView *)QRcodeImageView{ if (!_QRcodeImageView) { _QRcodeImageView=[[UIImageView alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(self.width-FITSIZE(19)-FITSIZE(QRWidth)), self.mainImageView.bottom+FitYHShareGoodsHeight(38), FitYHShareGoodsWidth(FITSIZE(QRWidth)), FitYHShareGoodsWidth(FITSIZE(QRWidth)))]; //1. 实例化二维码滤镜 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2. 恢复滤镜的默认属性 [filter setDefaults]; // 3. 将字符串转换成NSData NSString *urlStr =self.model.QRcodeImageUrl; NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding]; // 4. 通过KVO设置滤镜inputMessage数据 [filter setValue:data forKey:@"inputMessage"]; // 5. 获得滤镜输出的图像 CIImage *outputImage = [filter outputImage]; UIImage *img=[outputImage createNonInterpolatedUIImagewithSize:FITSIZE(QRWidth)]; [_QRcodeImageView setImage:img]; _QRcodeImageView.backgroundColor=[UIColor clearColor]; } return _QRcodeImageView; } -(UIImageView *)appImageView{ if (!_appImageView) { _appImageView=[[UIImageView alloc]init]; NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary]; NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject]; UIImage* image = [UIImage imageNamed:icon]; [_appImageView setImage:image]; } return _appImageView; } -(UILabel *)appTitle{ if (!_appTitle) { _appTitle=[[UILabel alloc]initWithFrame:CGRectMake(self.appImageView.right, self.appImageView.y, self.QRcodeImageView.right-self.appImageView.right-10,self.appImageView.height )]; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"]; _appTitle.text=app_Name; _appTitle.textColor=[UIColor YHColorWithHex:0x666666]; _appTitle.font=[UIFont systemFontOfSize:12.0f]; _appTitle.textAlignment=NSTextAlignmentRight; } return _appTitle; } - (UIActivityIndicatorView *)indicatorView { if (!_indicatorView) { _indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)]; _indicatorView.frame= CGRectMake(0, 0, 50, 50); _indicatorView.color = [UIColor homeRedColor]; _indicatorView.center = CGPointMake(self.width/2, self.height/2); _indicatorView.hidesWhenStopped = YES; } return _indicatorView; } - (UIImageView *)userIcon { if (!_userIcon) { _userIcon = [[UIImageView alloc] init]; _userIcon.layer.cornerRadius = FITHeightSIZE(15); _userIcon.backgroundColor = [UIColor yhGrayColor]; _userIcon.layer.masksToBounds = YES; _userIcon.hidden = YES; [_userIcon sd_setImageWithURL:[NSURL URLWithString:self.model.userinfo[@"headimg"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { count++; if (count>1) { if (self.imgSuccBlock) { self.imgSuccBlock(); } } }]; } return _userIcon; } - (UILabel *)nickName { if (!_nickName) { _nickName = [[UILabel alloc] init]; _nickName.textColor = [UIColor YHColorWithHex:0x333333]; _nickName.font = [UIFont systemFontOfSize:Fitsize(14)]; _nickName.text = self.model.userinfo[@"nickname"]; _nickName.hidden = YES; } return _nickName; } - (UILabel *)inviteCode { if (!_inviteCode) { _inviteCode = [[UILabel alloc] init]; _inviteCode.textColor = [UIColor YHColorWithHex:0x333333]; _inviteCode.font = [UIFont systemFontOfSize:Fitsize(12)]; _inviteCode.text = [NSString stringWithFormat:@"邀请码:%@",self.model.userinfo[@"invite_code"]]; _inviteCode.hidden = YES; } return _inviteCode; } @end