天天省钱快报

KBPopShareGoodView.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // KBPopShareGoodView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/7/10.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBPopShareGoodView.h"
  9. #define FitYHShareGoodsWidth(X) (X)
  10. #define FitYHShareGoodsHeight(X) (X)
  11. //#define FitYHShareGoodsWidth(X) (X)
  12. //#define FitYHShareGoodsHeight(X) (X)
  13. #define QRWidth (85)
  14. @interface KBPopShareGoodView()
  15. {
  16. NSInteger count;
  17. }
  18. //主图
  19. @property (strong, nonatomic) UIImageView *mainImageView;
  20. //商品标题
  21. @property (strong, nonatomic) UILabel *titleLabel;
  22. //商品券后价
  23. @property (strong, nonatomic) UILabel *ticketAfterPrice;
  24. //券钱
  25. @property (strong, nonatomic) UILabel *ticketPrice;
  26. //券钱
  27. @property (strong, nonatomic) UIImageView *ticketPriceBackImageView;
  28. //原始价格
  29. @property (strong, nonatomic) UILabel *originalPrice;
  30. //二维码图片
  31. @property (strong, nonatomic) UIImageView *QRcodeImageView;
  32. @property (strong, nonatomic) UIImageView *appImageView;
  33. @property (strong,nonatomic) UILabel *appTitle;
  34. @property (nonatomic, strong) UILabel *quanType;
  35. @property (nonatomic, strong) UILabel *commissionLabel;
  36. @property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
  37. @property (nonatomic, strong) UIImageView *userIcon;
  38. @property (nonatomic, strong) UILabel *nickName;
  39. @property (nonatomic, strong) UILabel *inviteCode;
  40. @end
  41. @implementation KBPopShareGoodView
  42. -(instancetype)initWithFrame:(CGRect)frame{
  43. if (self=[super initWithFrame:frame]) {
  44. }
  45. return self;
  46. }
  47. -(void)setModel:(KBShareGoodsModel *)model{
  48. _model=model;
  49. [self addUI];
  50. [self adjustUI];
  51. [self otherOP];
  52. }
  53. -(void)drawRect:(CGRect)rect{
  54. [super drawRect:rect];
  55. }
  56. -(void)addUI{
  57. [self addSubview:self.titleLabel];
  58. [self addSubview:self.mainImageView];
  59. [self addSubview:self.ticketAfterPrice];
  60. [self addSubview:self.originalPrice];
  61. [self addSubview:self.ticketPriceBackImageView];
  62. [self.ticketPriceBackImageView addSubview:self.ticketPrice];
  63. [self.ticketPriceBackImageView addSubview:self.quanType];
  64. [self addSubview:self.QRcodeImageView];
  65. [self addSubview:self.appImageView];
  66. [self addSubview:self.appTitle];
  67. [self addSubview:self.commissionLabel];
  68. [self addSubview:self.indicatorView];
  69. [self.indicatorView startAnimating];
  70. [self addSubview:self.userIcon];
  71. [self addSubview:self.nickName];
  72. [self addSubview:self.inviteCode];
  73. if (self.model.shareGoodsPurchaseType==YHShareGoodsPurchaseTypePrice) {
  74. self.ticketPrice.hidden=YES;
  75. self.ticketPriceBackImageView.hidden=YES;
  76. }else{
  77. self.ticketPrice.hidden=NO;
  78. self.ticketPriceBackImageView.hidden=NO;
  79. }
  80. }
  81. -(void)adjustUI{
  82. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(20)));
  84. make.top.mas_equalTo(FitYHShareGoodsHeight(26));
  85. make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-20)));
  86. }];
  87. [self.mainImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(20)));
  89. make.top.equalTo(self.titleLabel.mas_bottom).offset(FitYHShareGoodsHeight(22));
  90. make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-20)));
  91. make.height.mas_equalTo(FitYHShareGoodsWidth(self.width-2*FITSIZE(20)));
  92. }];
  93. [self.ticketAfterPrice mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.left.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(25)));
  95. make.top.equalTo(self.mainImageView.mas_bottom).offset(FitYHShareGoodsHeight(40));
  96. }];
  97. [self.originalPrice mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.left.mas_equalTo(self.ticketAfterPrice.mas_left);
  99. make.top.mas_equalTo(self.ticketAfterPrice.mas_bottom).mas_offset(FitYHShareGoodsHeight(20));
  100. // make.width.mas_equalTo(FITSIZE(74));
  101. }];
  102. [self.ticketPriceBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.left.equalTo(self.originalPrice.mas_left);
  104. make.top.mas_equalTo(self.originalPrice.mas_bottom).mas_offset(Fitsize(10));
  105. make.width.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(64)));
  106. make.height.mas_equalTo(FitYHShareGoodsHeight(FITSIZE(14)));
  107. }];
  108. [self.quanType mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.left.top.bottom.mas_equalTo(0);
  110. make.width.mas_equalTo(Fitsize(20));
  111. }];
  112. [self.ticketPrice mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.left.mas_equalTo(Fitsize(20));
  114. make.right.top.bottom.mas_equalTo(0);
  115. }];
  116. [self.QRcodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  117. make.right.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(-19)));
  118. make.top.equalTo(self.mainImageView.mas_bottom).offset(FitYHShareGoodsHeight(20));
  119. make.width.height.mas_equalTo(FITSIZE(QRWidth));
  120. }];
  121. [self.appImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  122. make.centerX.mas_equalTo(self.QRcodeImageView.mas_centerX);
  123. make.centerY.mas_equalTo(self.QRcodeImageView.mas_centerY);
  124. make.width.height.mas_equalTo(FitYHShareGoodsWidth(FITSIZE(18)));
  125. }];
  126. // [self.appTitle mas_makeConstraints:^(MASConstraintMaker *make) {
  127. // make.top.equalTo(self.appImageView.mas_top);
  128. // make.right.equalTo(self.QRcodeImageView.mas_right);
  129. // }];
  130. [self.commissionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.centerX.mas_equalTo(self.QRcodeImageView.mas_centerX);
  132. make.top.mas_equalTo(self.QRcodeImageView.mas_bottom).mas_offset(5);
  133. }];
  134. [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
  135. make.left.mas_equalTo(self.ticketAfterPrice.mas_left);
  136. make.top.mas_equalTo(self.ticketAfterPrice.mas_bottom).mas_offset(Fitsize(10));
  137. make.width.height.mas_equalTo(Fitsize(30));
  138. }];
  139. [self.nickName mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.left.mas_equalTo(self.userIcon.mas_right).mas_offset(5);
  141. make.centerY.mas_equalTo(self.userIcon.mas_centerY);
  142. make.width.mas_equalTo(Fitsize(100));
  143. make.height.mas_equalTo(Fitsize(20));
  144. }];
  145. [self.inviteCode mas_makeConstraints:^(MASConstraintMaker *make) {
  146. make.left.mas_equalTo(self.userIcon.mas_left);
  147. make.top.mas_equalTo(self.userIcon.mas_bottom).mas_equalTo(Fitsize(10));
  148. make.width.mas_equalTo(Fitsize(140));
  149. make.height.mas_equalTo(Fitsize(20));
  150. }];
  151. self.ticketPriceBackImageView.hidden = YES;
  152. }
  153. -(void)otherOP{
  154. self.backgroundColor=[UIColor yhGrayColor];
  155. [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) {
  156. count++;
  157. if (count>1) {
  158. if (self.imgSuccBlock) {
  159. self.imgSuccBlock();
  160. }
  161. }
  162. [self.indicatorView stopAnimating];
  163. }];
  164. }
  165. #pragma mark 懒加载
  166. -(UILabel *)titleLabel{
  167. if (!_titleLabel) {
  168. _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(20)), FitYHShareGoodsHeight(46), FitYHShareGoodsWidth(self.width-2*FITSIZE(20)), 0)];
  169. _titleLabel.font = [UIFont systemFontOfSize:14];
  170. NSTextAttachment *textAttach = [[NSTextAttachment alloc]init];
  171. UIImage *img;
  172. if (self.model.shareGoodsFromType==YHShareGoodsFromTypeTianMao) {
  173. img= [UIImage imageNamed:@"tm_shop"];
  174. }else if(self.model.shareGoodsFromType==YHShareGoodsFromTypeTaoBao){
  175. }
  176. if (img) {
  177. textAttach.image = img;
  178. }
  179. textAttach.bounds = CGRectMake(0, -3, img.size.width*0.9, img.size.height*0.9);
  180. NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",self.model.title]];
  181. NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:textAttach];
  182. [attri insertAttributedString:string atIndex:0];
  183. _titleLabel.attributedText = attri;
  184. // _titleLabel.text=@"虹小型电风扇迷你手持电扇USB学生宿舍 ";
  185. _titleLabel.numberOfLines=2;
  186. [_titleLabel sizeToFit];
  187. }
  188. return _titleLabel;
  189. }
  190. -(UIImageView *)mainImageView{
  191. if (!_mainImageView) {
  192. _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))))];
  193. }
  194. return _mainImageView;
  195. }
  196. -(UILabel *)ticketAfterPrice{
  197. if (!_ticketAfterPrice) {
  198. _ticketAfterPrice=[[UILabel alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(FITSIZE(15)), self.mainImageView.bottom+FitYHShareGoodsHeight(60), 0, 0)];
  199. if (self.model.shareGoodsPurchaseType==YHShareGoodsPurchaseTypeCoupons) {
  200. _ticketAfterPrice.text=[NSString stringWithFormat:@"券后¥%@",self.model.ticketAfterPrice];
  201. }else{
  202. _ticketAfterPrice.text=[NSString stringWithFormat:@"折后¥%@",self.model.ticketAfterPrice];
  203. }
  204. _ticketAfterPrice.font=[UIFont systemFontOfSize:14.0f];
  205. _ticketAfterPrice.textColor=[UIColor YHColorWithHex:0xEE1515];
  206. [_ticketAfterPrice sizeToFit];
  207. }
  208. return _ticketAfterPrice;
  209. }
  210. -(UILabel *)originalPrice{
  211. if (!_originalPrice) {
  212. _originalPrice=[[UILabel alloc]initWithFrame:CGRectMake(self.ticketAfterPrice.left, self.ticketAfterPrice.bottom+FitYHShareGoodsHeight(14), 0, 0)];
  213. NSString *price=[NSString stringWithFormat:@"原价¥%@",self.model.originalPrice];
  214. NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:price];
  215. [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, price.length)];
  216. [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor YHColorWithHex:0x999999] range:NSMakeRange(0, price.length)];
  217. [_originalPrice setAttributedText:attri];
  218. _originalPrice.textColor=[UIColor YHColorWithHex:0x999999];
  219. _originalPrice.font=[UIFont systemFontOfSize:13.0f];
  220. [_originalPrice sizeToFit];
  221. // _originalPrice.centerY = self.ticketAfterPrice.centerY;
  222. }
  223. return _originalPrice;
  224. }
  225. -(UIImageView *)ticketPriceBackImageView{
  226. if (!_ticketPriceBackImageView) {
  227. _ticketPriceBackImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.originalPrice.x, self.originalPrice.bottom+FitYHShareGoodsHeight(22), FitYHShareGoodsWidth(FITSIZE(64)), FitYHShareGoodsHeight(14))];
  228. // _ticketPriceBackImageView.backgroundColor=[UIColor blueColor];
  229. [_ticketPriceBackImageView setImage:[UIImage imageNamed:@"quan_detail"]];
  230. }
  231. return _ticketPriceBackImageView;
  232. }
  233. -(UILabel *)ticketPrice{
  234. if (!_ticketPrice) {
  235. _ticketPrice=[[UILabel alloc]init];
  236. _ticketPrice.textColor=[UIColor whiteColor];
  237. _ticketPrice.text=[NSString stringWithFormat:@"%@元",self.model.ticketPrice];
  238. _ticketPrice.font=[UIFont systemFontOfSize:Fitsize(10)];
  239. _ticketPrice.textAlignment=NSTextAlignmentCenter;
  240. }
  241. return _ticketPrice;
  242. }
  243. - (UILabel *)quanType {
  244. if (!_quanType) {
  245. _quanType = [[UILabel alloc] init];
  246. if (self.model.shareGoodsPurchaseType==YHShareGoodsPurchaseTypeCoupons) {
  247. _quanType.text=@"券";
  248. }else{
  249. _quanType.text=@"折";
  250. }
  251. _quanType.textColor=[UIColor whiteColor];
  252. _quanType.font=[UIFont systemFontOfSize:Fitsize(10)];
  253. _quanType.textAlignment=NSTextAlignmentCenter;
  254. }
  255. return _quanType;
  256. }
  257. - (UILabel *)commissionLabel {
  258. if (!_commissionLabel) {
  259. _commissionLabel = [[UILabel alloc] init];
  260. _commissionLabel.text = [NSString stringWithFormat:@"购买可赚¥%@",self.model.commissionPrice];
  261. _commissionLabel.textColor = [UIColor homeRedColor];
  262. _commissionLabel.font = [UIFont boldSystemFontOfSize:Fitsize(12)];
  263. _commissionLabel.text = [NSString stringWithFormat:@"扫我赚¥%@",self.model.commissionPrice];
  264. _commissionLabel.hidden = YES;
  265. }
  266. return _commissionLabel;
  267. }
  268. -(UIImageView *)QRcodeImageView{
  269. if (!_QRcodeImageView) {
  270. _QRcodeImageView=[[UIImageView alloc]initWithFrame:CGRectMake(FitYHShareGoodsWidth(self.width-FITSIZE(19)-FITSIZE(QRWidth)), self.mainImageView.bottom+FitYHShareGoodsHeight(38), FitYHShareGoodsWidth(FITSIZE(QRWidth)), FitYHShareGoodsWidth(FITSIZE(QRWidth)))];
  271. //1. 实例化二维码滤镜
  272. CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  273. // 2. 恢复滤镜的默认属性
  274. [filter setDefaults];
  275. // 3. 将字符串转换成NSData
  276. NSString *urlStr =self.model.QRcodeImageUrl;
  277. NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding];
  278. // 4. 通过KVO设置滤镜inputMessage数据
  279. [filter setValue:data forKey:@"inputMessage"];
  280. // 5. 获得滤镜输出的图像
  281. CIImage *outputImage = [filter outputImage];
  282. UIImage *img=[outputImage createNonInterpolatedUIImagewithSize:FITSIZE(QRWidth)];
  283. [_QRcodeImageView setImage:img];
  284. _QRcodeImageView.backgroundColor=[UIColor clearColor];
  285. }
  286. return _QRcodeImageView;
  287. }
  288. -(UIImageView *)appImageView{
  289. if (!_appImageView) {
  290. _appImageView=[[UIImageView alloc]init];
  291. NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
  292. NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
  293. UIImage* image = [UIImage imageNamed:icon];
  294. [_appImageView setImage:image];
  295. }
  296. return _appImageView;
  297. }
  298. -(UILabel *)appTitle{
  299. if (!_appTitle) {
  300. _appTitle=[[UILabel alloc]initWithFrame:CGRectMake(self.appImageView.right, self.appImageView.y, self.QRcodeImageView.right-self.appImageView.right-10,self.appImageView.height )];
  301. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  302. NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
  303. _appTitle.text=app_Name;
  304. _appTitle.textColor=[UIColor YHColorWithHex:0x666666];
  305. _appTitle.font=[UIFont systemFontOfSize:12.0f];
  306. _appTitle.textAlignment=NSTextAlignmentRight;
  307. }
  308. return _appTitle;
  309. }
  310. - (UIActivityIndicatorView *)indicatorView {
  311. if (!_indicatorView) {
  312. _indicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
  313. _indicatorView.frame= CGRectMake(0, 0, 50, 50);
  314. _indicatorView.color = [UIColor homeRedColor];
  315. _indicatorView.center = CGPointMake(self.width/2, self.height/2);
  316. _indicatorView.hidesWhenStopped = YES;
  317. }
  318. return _indicatorView;
  319. }
  320. - (UIImageView *)userIcon {
  321. if (!_userIcon) {
  322. _userIcon = [[UIImageView alloc] init];
  323. _userIcon.layer.cornerRadius = FITHeightSIZE(15);
  324. _userIcon.backgroundColor = [UIColor yhGrayColor];
  325. _userIcon.layer.masksToBounds = YES;
  326. _userIcon.hidden = YES;
  327. [_userIcon sd_setImageWithURL:[NSURL URLWithString:self.model.userinfo[@"headimg"]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  328. count++;
  329. if (count>1) {
  330. if (self.imgSuccBlock) {
  331. self.imgSuccBlock();
  332. }
  333. }
  334. }];
  335. }
  336. return _userIcon;
  337. }
  338. - (UILabel *)nickName {
  339. if (!_nickName) {
  340. _nickName = [[UILabel alloc] init];
  341. _nickName.textColor = [UIColor YHColorWithHex:0x333333];
  342. _nickName.font = [UIFont systemFontOfSize:Fitsize(14)];
  343. _nickName.text = self.model.userinfo[@"nickname"];
  344. _nickName.hidden = YES;
  345. }
  346. return _nickName;
  347. }
  348. - (UILabel *)inviteCode {
  349. if (!_inviteCode) {
  350. _inviteCode = [[UILabel alloc] init];
  351. _inviteCode.textColor = [UIColor YHColorWithHex:0x333333];
  352. _inviteCode.font = [UIFont systemFontOfSize:Fitsize(12)];
  353. _inviteCode.text = [NSString stringWithFormat:@"邀请码:%@",self.model.userinfo[@"invite_code"]];
  354. _inviteCode.hidden = YES;
  355. }
  356. return _inviteCode;
  357. }
  358. @end