省钱达人

DRShareGoodsView.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // DRShareGoodsView.m
  3. // YouHuiProject
  4. //
  5. // Created by jcymac on 2018/5/18.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRShareGoodsView.h"
  9. #define FitYHShareGoodsWidth(X) (X)
  10. #define FitYHShareGoodsHeight(X) (X)
  11. //#define FitYHShareGoodsWidth(X) (X)
  12. //#define FitYHShareGoodsHeight(X) (X)
  13. #define QRWidth (110)
  14. @interface DRShareGoodsView(){
  15. NSInteger count;
  16. }
  17. //主图
  18. @property (strong, nonatomic) UIImageView *mainImageView;
  19. //商品标题
  20. @property (strong, nonatomic) UILabel *titleLabel;
  21. //商品券后价
  22. @property (strong, nonatomic) UILabel *ticketAfterPrice;
  23. //券钱
  24. @property (strong, nonatomic) UILabel *ticketPrice;
  25. //券钱
  26. //原始价格
  27. @property (strong, nonatomic) UILabel *originalPrice;
  28. //二维码图片
  29. @property (strong, nonatomic) UIImageView *QRcodeImageView;
  30. @property (strong, nonatomic) UIImageView *appImageView;
  31. @property (nonatomic, strong) UIImageView *userIcon;
  32. @property (nonatomic, strong) UILabel *nickName;
  33. @property (nonatomic, strong) UILabel *inviteCode;
  34. @property (nonatomic, strong) UILabel *commissionLabel;
  35. @end
  36. @implementation DRShareGoodsView
  37. -(instancetype)initWithFrame:(CGRect)frame{
  38. if (self=[super initWithFrame:frame]) {
  39. }
  40. return self;
  41. }
  42. -(void)setModel:(DRShareGoodsModel *)model{
  43. _model=model;
  44. [self addUI];
  45. [self adjustUI];
  46. [self otherOP];
  47. }
  48. -(void)drawRect:(CGRect)rect{
  49. [super drawRect:rect];
  50. }
  51. -(void)addUI{
  52. [self addSubview:self.titleLabel];
  53. [self addSubview:self.mainImageView];
  54. [self addSubview:self.ticketAfterPrice];
  55. [self addSubview:self.originalPrice];
  56. [self addSubview:self.ticketPrice];
  57. [self addSubview:self.QRcodeImageView];
  58. [self.QRcodeImageView addSubview:self.appImageView];
  59. [self addSubview:self.commissionLabel];
  60. [self addSubview:self.userIcon];
  61. [self addSubview:self.nickName];
  62. [self addSubview:self.inviteCode];
  63. }
  64. -(void)adjustUI{
  65. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  66. make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(20)));
  67. make.top.mas_equalTo(FitYHShareGoodsHeight(46));
  68. make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-20)));
  69. }];
  70. [self.mainImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(20)));
  72. make.top.equalTo(self.titleLabel.mas_bottom).offset(FitYHShareGoodsHeight(42));
  73. make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-20)));
  74. make.height.mas_equalTo(FitYHShareGoodsWidth(self.width-2*FITSIZE(20)));
  75. }];
  76. [self.ticketAfterPrice mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(50)));
  78. make.top.equalTo(self.mainImageView.mas_bottom).offset(FitYHShareGoodsHeight(40));
  79. }];
  80. [self.originalPrice mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.left.mas_equalTo(self.ticketAfterPrice.mas_right).mas_offset(5);
  82. make.centerY.mas_equalTo(self.ticketAfterPrice.mas_centerY);
  83. // make.width.mas_equalTo(FITSIZE(74));
  84. }];
  85. [self.QRcodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  86. make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-19)));
  87. make.top.equalTo(self.mainImageView.mas_bottom).offset(FitYHShareGoodsHeight(38));
  88. make.width.height.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(QRWidth)));
  89. }];
  90. [self.appImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.left.equalTo(self.QRcodeImageView.mas_left);
  92. make.top.equalTo(self.QRcodeImageView.mas_bottom).offset(FitYHShareGoodsHeight(12));
  93. make.width.height.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(18)));
  94. }];
  95. [self.commissionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.centerX.mas_equalTo(self.QRcodeImageView.mas_centerX);
  97. make.top.mas_equalTo(self.QRcodeImageView.mas_bottom).mas_offset(5);
  98. }];
  99. [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  100. make.left.mas_equalTo(self.ticketAfterPrice.mas_left);
  101. make.top.mas_equalTo(self.ticketAfterPrice.mas_bottom).mas_offset(Fitsize(10));
  102. make.width.height.mas_equalTo(Fitsize(30));
  103. }];
  104. [self.nickName mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.left.mas_equalTo(self.userIcon.mas_right).mas_offset(5);
  106. make.centerY.mas_equalTo(self.userIcon.mas_centerY);
  107. make.width.mas_equalTo(Fitsize(100));
  108. make.height.mas_equalTo(Fitsize(20));
  109. }];
  110. [self.inviteCode mas_makeConstraints:^(MASConstraintMaker *make) {
  111. make.left.mas_equalTo(self.userIcon.mas_left);
  112. make.top.mas_equalTo(self.userIcon.mas_bottom).mas_equalTo(Fitsize(10));
  113. make.width.mas_equalTo(Fitsize(100));
  114. make.height.mas_equalTo(Fitsize(20));
  115. }];
  116. }
  117. - (void)setUserInfo:(NSDictionary *)userInfo {
  118. _userInfo = userInfo;
  119. [self.userIcon sd_setImageWithURL:[NSURL URLWithString:userInfo[@"headimg"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  120. count++;
  121. if (count > 1) {
  122. if (self.imgSuccBlock) {
  123. self.imgSuccBlock();
  124. }
  125. }
  126. }];
  127. self.nickName.text = userInfo[@"nickname"];
  128. self.inviteCode.text= [NSString stringWithFormat:@"邀请码:%@",userInfo[@"invite_code"]];
  129. }
  130. -(void)otherOP{
  131. self.backgroundColor=[UIColor yhGrayColor];
  132. [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) {
  133. count++;
  134. if (count > 1) {
  135. if (self.imgSuccBlock) {
  136. self.imgSuccBlock();
  137. }
  138. }
  139. }];
  140. }
  141. #pragma mark 懒加载
  142. -(UILabel *)titleLabel{
  143. if (!_titleLabel) {
  144. _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(20)), FitYHShareGoodsHeight(46), FitYHShareGoodsWidth(self.width-2*FITSIZE(20)), 0)];
  145. NSTextAttachment *textAttach = [[NSTextAttachment alloc]init];
  146. UIImage *img;
  147. if (self.model.shareGoodsFromType==YHShareGoodsFromTypeTianMao) {
  148. img= [UIImage imageNamed:@"share_title_tm"];
  149. }else if(self.model.shareGoodsFromType==YHShareGoodsFromTypeTaoBao){
  150. img= [UIImage imageNamed:@"share_title_tb"];
  151. }
  152. if (img) {
  153. textAttach.image = img;
  154. }
  155. NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",self.model.title]];
  156. NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:textAttach];
  157. [attri insertAttributedString:string atIndex:0];
  158. _titleLabel.attributedText = attri;
  159. // _titleLabel.text=@"虹小型电风扇迷你手持电扇USB学生宿舍 ";
  160. _titleLabel.numberOfLines=2;
  161. [_titleLabel sizeToFit];
  162. }
  163. return _titleLabel;
  164. }
  165. -(UIImageView *)mainImageView{
  166. if (!_mainImageView) {
  167. _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))))];
  168. }
  169. return _mainImageView;
  170. }
  171. -(UILabel *)ticketAfterPrice{
  172. if (!_ticketAfterPrice) {
  173. _ticketAfterPrice=[[UILabel alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(15)), self.mainImageView.bottom+FitYHShareGoodsHeight(40), 0, 0)];
  174. if (self.model.shareGoodsPurchaseType==YHShareGoodsPurchaseTypeCoupons) {
  175. _ticketAfterPrice.text=[NSString stringWithFormat:@"券后价¥%@",self.model.ticketAfterPrice];
  176. }else{
  177. _ticketAfterPrice.text=[NSString stringWithFormat:@"折后价¥%@",self.model.ticketAfterPrice];
  178. }
  179. _ticketAfterPrice.font=[UIFont systemFontOfSize:15.0f];
  180. _ticketAfterPrice.textColor=[UIColor YHColorWithHex:0xEE1515];
  181. [_ticketAfterPrice sizeToFit];
  182. }
  183. return _ticketAfterPrice;
  184. }
  185. -(UILabel *)originalPrice{
  186. if (!_originalPrice) {
  187. _originalPrice=[[UILabel alloc]initWithFrame:CGRectMake(self.ticketAfterPrice.right+5, self.ticketAfterPrice.y, 0, 0)];
  188. // _originalPrice.centerY = self.ticketAfterPrice.centerY;
  189. NSString *price=[NSString stringWithFormat:@"¥%@",self.model.originalPrice];
  190. NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:price];
  191. [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, price.length)];
  192. [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor YHColorWithHex:0x999999] range:NSMakeRange(0, price.length)];
  193. [_originalPrice setAttributedText:attri];
  194. _originalPrice.textColor=[UIColor YHColorWithHex:0x999999];
  195. _originalPrice.font=[UIFont systemFontOfSize:13.0f];
  196. [_originalPrice sizeToFit];
  197. }
  198. return _originalPrice;
  199. }
  200. -(UIImageView *)QRcodeImageView{
  201. if (!_QRcodeImageView) {
  202. _QRcodeImageView=[[UIImageView alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(self.width-FITSIZE(19)-FITSIZE(QRWidth)), self.mainImageView.bottom+FitYHShareGoodsHeight(38), FitYHShareGoodsWidth(FITSIZE(QRWidth)), FitYHShareGoodsWidth(FITSIZE(QRWidth)))];
  203. //1. 实例化二维码滤镜
  204. CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  205. // 2. 恢复滤镜的默认属性
  206. [filter setDefaults];
  207. // 3. 将字符串转换成NSData
  208. NSString *urlStr =self.model.QRcodeImageUrl;
  209. NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding];
  210. // 4. 通过KVO设置滤镜inputMessage数据
  211. [filter setValue:data forKey:@"inputMessage"];
  212. // 5. 获得滤镜输出的图像
  213. CIImage *outputImage = [filter outputImage];
  214. UIImage *img=[outputImage createNonInterpolatedUIImagewithSize:FITSIZE(QRWidth)];
  215. [_QRcodeImageView setImage:img];
  216. _QRcodeImageView.backgroundColor=[UIColor clearColor];
  217. }
  218. return _QRcodeImageView;
  219. }
  220. -(UIImageView *)appImageView{
  221. if (!_appImageView) {
  222. _appImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.QRcodeImageView.x+10, self.QRcodeImageView.bottom+FitYHShareGoodsHeight(12), FitYHShareGoodsWidth(FITSIZE(18)), FitYHShareGoodsWidth(FITSIZE(18)))];
  223. _appImageView.center = CGPointMake(self.QRcodeImageView.width/2, self.QRcodeImageView.height/2);
  224. [_appImageView setImage:[UIImage imageNamed:@"share_appicon"]];
  225. }
  226. return _appImageView;
  227. }
  228. - (UIImageView *)userIcon {
  229. if (!_userIcon) {
  230. _userIcon = [[UIImageView alloc] initWithFrame:CGRectMake(self.ticketAfterPrice.left, self.ticketAfterPrice.bottom+15, Fitsize(30), Fitsize(30))];
  231. _userIcon.layer.cornerRadius = FITHeightSIZE(15);
  232. _userIcon.backgroundColor = [UIColor yhGrayColor];
  233. _userIcon.layer.masksToBounds = YES;
  234. }
  235. return _userIcon;
  236. }
  237. - (UILabel *)nickName {
  238. if (!_nickName) {
  239. _nickName = [[UILabel alloc] initWithFrame:CGRectMake(self.userIcon.right+5, self.userIcon.y, Fitsize(100), Fitsize(20))];
  240. _nickName.textColor = [UIColor YHColorWithHex:0x333333];
  241. _nickName.font = [UIFont systemFontOfSize:Fitsize(14)];
  242. _nickName.centerY = self.userIcon.centerY;
  243. // _nickName.text = self.model.nickname;
  244. }
  245. return _nickName;
  246. }
  247. - (UILabel *)inviteCode {
  248. if (!_inviteCode) {
  249. _inviteCode = [[UILabel alloc] initWithFrame:CGRectMake(self.userIcon.left, self.userIcon.bottom+15, Fitsize(150), Fitsize(20))];
  250. _inviteCode.textColor = [UIColor YHColorWithHex:0x333333];
  251. _inviteCode.font = [UIFont systemFontOfSize:Fitsize(14)];
  252. // _inviteCode.text = [NSString stringWithFormat:@"邀请码:%@",self.model.inviteCode];
  253. }
  254. return _inviteCode;
  255. }
  256. - (UILabel *)commissionLabel {
  257. if (!_commissionLabel) {
  258. _commissionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.QRcodeImageView.bottom+8, Fitsize(100), Fitsize(15))];
  259. _commissionLabel.textColor = [UIColor homeRedColor];
  260. _commissionLabel.font = [UIFont boldSystemFontOfSize:Fitsize(12)];
  261. _commissionLabel.text = [NSString stringWithFormat:@"扫我赚¥%@",self.model.commission_price];
  262. _commissionLabel.textAlignment = NSTextAlignmentCenter;
  263. _commissionLabel.hidden = YES;
  264. _commissionLabel.centerX = self.QRcodeImageView.centerX;
  265. }
  266. return _commissionLabel;
  267. }
  268. @end