《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRShareGoodViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // DRShareGoodViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/12/28.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRShareGoodViewController.h"
  9. #import "CCCopyLabel.h"
  10. #import "DRImageContainerView.h"
  11. #import "DRShareImgPopView.h"
  12. #import "DRShareImageCellModel.h"
  13. #import "DRBottomShareView.h"
  14. #import "DRShareDesView.h"
  15. #import "DRPopShareGoodView.h"
  16. #import "DRShareManager.h"
  17. #import <AssetsLibrary/AssetsLibrary.h>
  18. #import <AssetsLibrary/AssetsLibrary.h>
  19. #import <Photos/Photos.h>
  20. @interface DRShareGoodViewController ()<BottomShareViewDelegate>
  21. {
  22. UIImage *_shareImg;//海报图片
  23. }
  24. @property (nonatomic, strong) NSMutableArray *imageArray;
  25. @property (nonatomic, strong) UIScrollView *scrollView;
  26. @property (nonatomic, strong) UIView *containerView;
  27. @property (nonatomic, strong) CCCopyLabel *contentLabel;
  28. @property (nonatomic, strong) UIView *line;
  29. @property (nonatomic, strong) DRImageContainerView *imgContainer;
  30. @property (nonatomic, strong) UIButton *showCommissionBtn;
  31. @property (nonatomic, strong) DRBottomShareView *bottomShareView;
  32. @property (nonatomic, strong) DRShareDesView *shareDesView;
  33. @property (nonatomic, strong) DRPopShareGoodView *shareGoodView;
  34. @end
  35. @implementation DRShareGoodViewController
  36. - (void)viewWillDisappear:(BOOL)animated {
  37. [super viewWillDisappear:animated];
  38. [SVProgressHUD dismiss];
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. [self configNavigationBar];
  43. [self initSubViews];
  44. [self requestData];
  45. }
  46. - (void)configNavigationBar {
  47. [self.navigationBar setNavTitle:@"分享商品"];
  48. self.navigationBar.backgroundColor = [UIColor changeColor];
  49. self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor];
  50. UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  51. [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal];
  52. [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  53. [self.navigationBar setCustomLeftButtons:@[leftBtn]];
  54. }
  55. - (void)backAction {
  56. [self.navigationController popViewControllerAnimated:YES];
  57. }
  58. - (void)requestData {
  59. for (NSString *imgUrl in self.goodModel.small_images) {
  60. DRShareImageCellModel *model = [[DRShareImageCellModel alloc] init];
  61. model.imgUrl = imgUrl;
  62. [self.imageArray addObject:model];
  63. }
  64. DRShareImageCellModel *firstModel = [[DRShareImageCellModel alloc] init];
  65. firstModel.isSelected = YES;
  66. [self.imageArray insertObject:firstModel atIndex:0];
  67. self.shareGoodView = [[DRPopShareGoodView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, Fitsize(254), Fitsize(455))];
  68. self.shareGoodView.backgroundColor = [UIColor whiteColor];
  69. [self.view addSubview:self.shareGoodView];
  70. __weak typeof(self) weakSelf = self;
  71. self.shareGoodView.imgSuccBlock = ^{
  72. UIImage *resultImg = [weakSelf.shareGoodView changeToImage];
  73. _shareImg = resultImg;
  74. firstModel.shareImg = resultImg;
  75. [weakSelf.imgContainer reloadWithDataArray:weakSelf.imageArray];
  76. };
  77. [self.imgContainer reloadWithDataArray:self.imageArray];
  78. self.shareGoodView.model = self.goodModel;
  79. }
  80. - (void)initSubViews {
  81. [self.view addSubview:self.scrollView];
  82. [self.scrollView addSubview:self.containerView];
  83. [self.containerView addSubview:self.contentLabel];
  84. [self.containerView addSubview:self.line];
  85. [self.containerView addSubview:self.imgContainer];
  86. [self.containerView addSubview:self.showCommissionBtn];
  87. [self.view addSubview:self.shareDesView];
  88. [self.view addSubview:self.bottomShareView];
  89. [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  90. make.top.mas_equalTo(NavBarHeight);
  91. make.left.mas_equalTo(0);
  92. make.width.mas_equalTo(SCREEN_WIDTH);
  93. make.height.mas_equalTo(SCREEN_HEIGHT-NavBarHeight);
  94. }];
  95. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.edges.equalTo(self.scrollView);
  97. make.width.equalTo(self.scrollView);
  98. }];
  99. [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  100. make.left.top.mas_equalTo(15);
  101. make.right.mas_equalTo(-15);
  102. }];
  103. [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.left.mas_equalTo(15);
  105. make.right.mas_equalTo(-15);
  106. make.height.mas_equalTo(1);
  107. make.top.mas_equalTo(self.contentLabel.mas_bottom).mas_offset(15);
  108. }];
  109. [self.imgContainer mas_makeConstraints:^(MASConstraintMaker *make) {
  110. make.left.right.mas_equalTo(0);
  111. make.top.mas_equalTo(self.line.mas_bottom).mas_offset(10);
  112. make.height.mas_equalTo(150);
  113. }];
  114. [self.showCommissionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  115. make.left.mas_equalTo(15);
  116. make.top.mas_equalTo(self.imgContainer.mas_bottom).mas_offset(20);
  117. make.width.mas_equalTo(200);
  118. make.height.mas_equalTo(30);
  119. }];
  120. [self.bottomShareView mas_makeConstraints:^(MASConstraintMaker *make) {
  121. make.left.mas_equalTo(0);
  122. make.bottom.mas_equalTo(-BottomMargin);
  123. make.width.mas_equalTo(SCREEN_WIDTH);
  124. make.height.mas_equalTo(SCREEN_WIDTH/4);
  125. }];
  126. [self.shareDesView mas_makeConstraints:^(MASConstraintMaker *make) {
  127. make.left.mas_equalTo(0);
  128. make.width.mas_equalTo(SCREEN_WIDTH);
  129. make.bottom.mas_equalTo(self.bottomShareView.mas_top);
  130. make.height.mas_equalTo(40);
  131. }];
  132. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  133. make.bottom.mas_equalTo(self.showCommissionBtn.mas_bottom).mas_offset(160);// 这里放最后一个view的底部
  134. }];
  135. }
  136. - (void)showSelectAction:(UIButton *)sender {
  137. sender.selected = !sender.selected;
  138. if (sender.selected) {
  139. NSString *text = [NSString stringWithFormat:@"%@\n--------\n【在售价】%@元\n【券后价】%@元\n【下载猎豆优选再省】%@元\n--------\n复制这条信息¥ %@ ¥ \n打开【手机淘宝】即可查看",self.goodModel.title,self.goodModel.price,self.goodModel.discount_price,self.goodModel.super_commission,self.goodModel.tkl];
  140. self.contentLabel.text = text;
  141. }else {
  142. NSString *text = [NSString stringWithFormat:@"%@\n--------\n【在售价】%@元\n【券后价】%@元\n--------\n复制这条信息¥ %@ ¥ \n打开【手机淘宝】即可查看",self.goodModel.title,self.goodModel.price,self.goodModel.discount_price,self.goodModel.tkl];
  143. self.contentLabel.text = text;
  144. }
  145. }
  146. #pragma mark ----- bottomShareViewDelegate -----
  147. - (void)BottomShareViewDidSelectedIndex:(NSInteger)index {
  148. switch (index) {
  149. case 0:
  150. //分享到朋友圈
  151. [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine];
  152. [MobClick event:NewGoodDetailCircleShare label:@"朋友圈"];
  153. break;
  154. case 1:
  155. //分享到微信
  156. [self shareImgToWeChat:UMSocialPlatformType_WechatSession];
  157. [MobClick event:NewGoodDetailFriendShare label:@"微信好友"];
  158. break;
  159. case 2:
  160. //复制淘口令
  161. [self copyCode];
  162. break;
  163. case 3:
  164. //保存图片
  165. [self saveImage];
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. /**
  172. 分享
  173. */
  174. - (void)shareImgToWeChat:(UMSocialPlatformType)platformType {
  175. if (self.shareResult) {
  176. self.shareResult();
  177. }
  178. for (DRShareImageCellModel *model in self.imageArray) {
  179. if (model.isSelected) {
  180. if (model.imgUrl) {
  181. [DRShareManager shareImageWithplatformType:platformType withImg:model.imgUrl complete:^(id result, NSError *error) {
  182. }];
  183. return;
  184. }
  185. //海报
  186. if (model.shareImg) {
  187. [DRShareManager shareImageWithplatformType:platformType withImg:model.shareImg complete:^(id result, NSError *error) {
  188. }];
  189. }else {
  190. [XHToast showCenterWithText:@"图片生成中"];
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. 复制邀请码
  197. */
  198. - (void)copyCode {
  199. if (self.goodModel.tkl && self.contentLabel.text) {
  200. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  201. pasteboard.string = self.contentLabel.text;
  202. [XHToast showCenterWithText:@"复制成功"];
  203. }else {
  204. [XHToast showCenterWithText:@"复制失败"];
  205. }
  206. }
  207. - (void)saveImage {
  208. if (_shareImg) {
  209. __block NSInteger count = 0;
  210. [SVProgressHUD showProgress:count/self.imageArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)self.imageArray.count]];
  211. for (int i = 0; i < self.imageArray.count; i++) {
  212. DRShareImageCellModel *model = self.imageArray[i];
  213. if (model.shareImg) {
  214. __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
  215. [lib writeImageToSavedPhotosAlbum:model.shareImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
  216. if (error) {
  217. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
  218. [alert show];
  219. }else {
  220. count++;
  221. [SVProgressHUD showProgress:count/self.imageArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)self.imageArray.count]];
  222. if (count == self.imageArray.count) {
  223. [SVProgressHUD dismiss];
  224. [XHToast showCenterWithText:@"保存成功"];
  225. }
  226. }
  227. lib = nil;
  228. }];
  229. }else {
  230. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.imgUrl] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  231. __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
  232. [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
  233. if (error) {
  234. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
  235. [alert show];
  236. }else {
  237. count++;
  238. [SVProgressHUD showProgress:count/self.imageArray.count status:[NSString stringWithFormat:@"%ld/%ld",count,self.imageArray.count]];
  239. if (count == self.imageArray.count) {
  240. [SVProgressHUD dismiss];
  241. [XHToast showCenterWithText:@"保存成功"];
  242. }
  243. }
  244. lib = nil;
  245. }];
  246. }];
  247. }
  248. }
  249. }else {
  250. [XHToast showCenterWithText:@"图片生成中"];
  251. }
  252. }
  253. #pragma mark -----
  254. - (CCCopyLabel *)contentLabel {
  255. if (!_contentLabel) {
  256. _contentLabel = [[CCCopyLabel alloc] init];
  257. _contentLabel.font = [UIFont systemFontOfSize:15];
  258. _contentLabel.numberOfLines = 0;
  259. NSString *text = [NSString stringWithFormat:@"%@\n--------\n【在售价】%@元\n【券后价】%@元\n--------\n复制这条信息¥ %@ ¥ \n打开【手机淘宝】即可查看",self.goodModel.title,self.goodModel.price,self.goodModel.discount_price,self.goodModel.tkl];
  260. _contentLabel.text = text;
  261. }
  262. return _contentLabel;
  263. }
  264. - (UIView *)line {
  265. if (!_line) {
  266. _line = [[UIView alloc] init];
  267. _line.backgroundColor = [UIColor YHColorWithHex:0xE5E4E6];
  268. }
  269. return _line;
  270. }
  271. - (UIScrollView *)scrollView {
  272. if (!_scrollView) {
  273. _scrollView = [[UIScrollView alloc] init];
  274. }
  275. return _scrollView;
  276. }
  277. - (UIView *)containerView {
  278. if (!_containerView) {
  279. _containerView = [UIView new];
  280. }
  281. return _containerView;
  282. }
  283. - (DRImageContainerView *)imgContainer {
  284. if (!_imgContainer) {
  285. _imgContainer = [DRImageContainerView new];
  286. }
  287. return _imgContainer;
  288. }
  289. - (UIButton *)showCommissionBtn {
  290. if (!_showCommissionBtn) {
  291. _showCommissionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  292. [_showCommissionBtn setTitle:@"显示猎豆优选收益" forState:UIControlStateNormal];
  293. _showCommissionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  294. [_showCommissionBtn setTitleColor:[UIColor YHColorWithHex:0x393939] forState:UIControlStateNormal];
  295. [_showCommissionBtn setImage:[UIImage imageNamed:@"showSel_n"] forState:UIControlStateNormal];
  296. [_showCommissionBtn setImage:[UIImage imageNamed:@"showSel_s"] forState:UIControlStateSelected];
  297. _showCommissionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  298. [_showCommissionBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:4];
  299. [_showCommissionBtn addTarget:self action:@selector(showSelectAction:) forControlEvents:UIControlEventTouchUpInside];
  300. }
  301. return _showCommissionBtn;
  302. }
  303. - (NSMutableArray *)imageArray {
  304. if (!_imageArray) {
  305. _imageArray = [NSMutableArray array];
  306. }
  307. return _imageArray;
  308. }
  309. - (DRBottomShareView *)bottomShareView {
  310. if (!_bottomShareView) {
  311. _bottomShareView = [[DRBottomShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/4)];
  312. _bottomShareView.delegate = self;
  313. }
  314. return _bottomShareView;
  315. }
  316. - (DRShareDesView *)shareDesView {
  317. if (!_shareDesView) {
  318. _shareDesView = [[DRShareDesView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) commissPrice:self.goodModel.super_commission];
  319. }
  320. return _shareDesView;
  321. }
  322. @end