// // DRShareGoodViewController.m // YouHuiProject // // Created by 小花 on 2018/12/28. // Copyright © 2018年 kuxuan. All rights reserved. // #import "DRShareGoodViewController.h" #import "CCCopyLabel.h" #import "DRImageContainerView.h" #import "DRShareImgPopView.h" #import "DRShareImageCellModel.h" #import "DRBottomShareView.h" #import "DRShareDesView.h" #import "DRPopShareGoodView.h" #import "DRShareManager.h" #import #import #import @interface DRShareGoodViewController () { UIImage *_shareImg;//海报图片 } @property (nonatomic, strong) NSMutableArray *imageArray; @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) UIView *containerView; @property (nonatomic, strong) CCCopyLabel *contentLabel; @property (nonatomic, strong) UIView *line; @property (nonatomic, strong) DRImageContainerView *imgContainer; @property (nonatomic, strong) UIButton *showCommissionBtn; @property (nonatomic, strong) DRBottomShareView *bottomShareView; @property (nonatomic, strong) DRShareDesView *shareDesView; @property (nonatomic, strong) DRPopShareGoodView *shareGoodView; @end @implementation DRShareGoodViewController - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [SVProgressHUD dismiss]; } - (void)viewDidLoad { [super viewDidLoad]; [self configNavigationBar]; [self initSubViews]; [self requestData]; } - (void)configNavigationBar { [self.navigationBar setNavTitle:@"分享商品"]; self.navigationBar.backgroundColor = [UIColor changeColor]; self.navigationBar.navTitleLabel.textColor = [UIColor whiteColor]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back_white"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)requestData { for (NSString *imgUrl in self.goodModel.small_images) { DRShareImageCellModel *model = [[DRShareImageCellModel alloc] init]; model.imgUrl = imgUrl; [self.imageArray addObject:model]; } DRShareImageCellModel *firstModel = [[DRShareImageCellModel alloc] init]; firstModel.isSelected = YES; [self.imageArray insertObject:firstModel atIndex:0]; self.shareGoodView = [[DRPopShareGoodView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, Fitsize(254), Fitsize(455))]; self.shareGoodView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.shareGoodView]; __weak typeof(self) weakSelf = self; self.shareGoodView.imgSuccBlock = ^{ UIImage *resultImg = [weakSelf.shareGoodView changeToImage]; _shareImg = resultImg; firstModel.shareImg = resultImg; [weakSelf.imgContainer reloadWithDataArray:weakSelf.imageArray]; }; [self.imgContainer reloadWithDataArray:self.imageArray]; self.shareGoodView.model = self.goodModel; } - (void)initSubViews { [self.view addSubview:self.scrollView]; [self.scrollView addSubview:self.containerView]; [self.containerView addSubview:self.contentLabel]; [self.containerView addSubview:self.line]; [self.containerView addSubview:self.imgContainer]; [self.containerView addSubview:self.showCommissionBtn]; [self.view addSubview:self.shareDesView]; [self.view addSubview:self.bottomShareView]; [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(NavBarHeight); make.left.mas_equalTo(0); make.width.mas_equalTo(SCREEN_WIDTH); make.height.mas_equalTo(SCREEN_HEIGHT-NavBarHeight); }]; [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.scrollView); make.width.equalTo(self.scrollView); }]; [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.mas_equalTo(15); make.right.mas_equalTo(-15); }]; [self.line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.right.mas_equalTo(-15); make.height.mas_equalTo(1); make.top.mas_equalTo(self.contentLabel.mas_bottom).mas_offset(15); }]; [self.imgContainer mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(0); make.top.mas_equalTo(self.line.mas_bottom).mas_offset(10); make.height.mas_equalTo(150); }]; [self.showCommissionBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.top.mas_equalTo(self.imgContainer.mas_bottom).mas_offset(20); make.width.mas_equalTo(200); make.height.mas_equalTo(30); }]; [self.bottomShareView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.bottom.mas_equalTo(-BottomMargin); make.width.mas_equalTo(SCREEN_WIDTH); make.height.mas_equalTo(SCREEN_WIDTH/4); }]; [self.shareDesView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.width.mas_equalTo(SCREEN_WIDTH); make.bottom.mas_equalTo(self.bottomShareView.mas_top); make.height.mas_equalTo(40); }]; [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(self.showCommissionBtn.mas_bottom).mas_offset(160);// 这里放最后一个view的底部 }]; } - (void)showSelectAction:(UIButton *)sender { sender.selected = !sender.selected; if (sender.selected) { 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]; self.contentLabel.text = text; }else { NSString *text = [NSString stringWithFormat:@"%@\n--------\n【在售价】%@元\n【券后价】%@元\n--------\n复制这条信息¥ %@ ¥ \n打开【手机淘宝】即可查看",self.goodModel.title,self.goodModel.price,self.goodModel.discount_price,self.goodModel.tkl]; self.contentLabel.text = text; } } #pragma mark ----- bottomShareViewDelegate ----- - (void)BottomShareViewDidSelectedIndex:(NSInteger)index { switch (index) { case 0: //分享到朋友圈 [self shareImgToWeChat:UMSocialPlatformType_WechatTimeLine]; [MobClick event:NewGoodDetailCircleShare label:@"朋友圈"]; break; case 1: //分享到微信 [self shareImgToWeChat:UMSocialPlatformType_WechatSession]; [MobClick event:NewGoodDetailFriendShare label:@"微信好友"]; break; case 2: //复制淘口令 [self copyCode]; break; case 3: //保存图片 [self saveImage]; break; default: break; } } /** 分享 */ - (void)shareImgToWeChat:(UMSocialPlatformType)platformType { if (self.shareResult) { self.shareResult(); } for (DRShareImageCellModel *model in self.imageArray) { if (model.isSelected) { if (model.imgUrl) { [DRShareManager shareImageWithplatformType:platformType withImg:model.imgUrl complete:^(id result, NSError *error) { }]; return; } //海报 if (model.shareImg) { [DRShareManager shareImageWithplatformType:platformType withImg:model.shareImg complete:^(id result, NSError *error) { }]; }else { [XHToast showCenterWithText:@"图片生成中"]; } } } } /** 复制邀请码 */ - (void)copyCode { if (self.goodModel.tkl && self.contentLabel.text) { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = self.contentLabel.text; [XHToast showCenterWithText:@"复制成功"]; }else { [XHToast showCenterWithText:@"复制失败"]; } } - (void)saveImage { if (_shareImg) { __block NSInteger count = 0; [SVProgressHUD showProgress:count/self.imageArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)self.imageArray.count]]; for (int i = 0; i < self.imageArray.count; i++) { DRShareImageCellModel *model = self.imageArray[i]; if (model.shareImg) { __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; [lib writeImageToSavedPhotosAlbum:model.shareImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { if (error) { UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; [alert show]; }else { count++; [SVProgressHUD showProgress:count/self.imageArray.count status:[NSString stringWithFormat:@"%ld/%lu",(long)count,(unsigned long)self.imageArray.count]]; if (count == self.imageArray.count) { [SVProgressHUD dismiss]; [XHToast showCenterWithText:@"保存成功"]; } } lib = nil; }]; }else { [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.imgUrl] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { if (error) { UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您先去设置允许APP访问您的相册 设置>隐私>照片" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil]; [alert show]; }else { count++; [SVProgressHUD showProgress:count/self.imageArray.count status:[NSString stringWithFormat:@"%ld/%ld",count,self.imageArray.count]]; if (count == self.imageArray.count) { [SVProgressHUD dismiss]; [XHToast showCenterWithText:@"保存成功"]; } } lib = nil; }]; }]; } } }else { [XHToast showCenterWithText:@"图片生成中"]; } } #pragma mark ----- - (CCCopyLabel *)contentLabel { if (!_contentLabel) { _contentLabel = [[CCCopyLabel alloc] init]; _contentLabel.font = [UIFont systemFontOfSize:15]; _contentLabel.numberOfLines = 0; NSString *text = [NSString stringWithFormat:@"%@\n--------\n【在售价】%@元\n【券后价】%@元\n--------\n复制这条信息¥ %@ ¥ \n打开【手机淘宝】即可查看",self.goodModel.title,self.goodModel.price,self.goodModel.discount_price,self.goodModel.tkl]; _contentLabel.text = text; } return _contentLabel; } - (UIView *)line { if (!_line) { _line = [[UIView alloc] init]; _line.backgroundColor = [UIColor YHColorWithHex:0xE5E4E6]; } return _line; } - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc] init]; } return _scrollView; } - (UIView *)containerView { if (!_containerView) { _containerView = [UIView new]; } return _containerView; } - (DRImageContainerView *)imgContainer { if (!_imgContainer) { _imgContainer = [DRImageContainerView new]; } return _imgContainer; } - (UIButton *)showCommissionBtn { if (!_showCommissionBtn) { _showCommissionBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_showCommissionBtn setTitle:@"显示猎豆优选收益" forState:UIControlStateNormal]; _showCommissionBtn.titleLabel.font = [UIFont systemFontOfSize:14]; [_showCommissionBtn setTitleColor:[UIColor YHColorWithHex:0x393939] forState:UIControlStateNormal]; [_showCommissionBtn setImage:[UIImage imageNamed:@"showSel_n"] forState:UIControlStateNormal]; [_showCommissionBtn setImage:[UIImage imageNamed:@"showSel_s"] forState:UIControlStateSelected]; _showCommissionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [_showCommissionBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:4]; [_showCommissionBtn addTarget:self action:@selector(showSelectAction:) forControlEvents:UIControlEventTouchUpInside]; } return _showCommissionBtn; } - (NSMutableArray *)imageArray { if (!_imageArray) { _imageArray = [NSMutableArray array]; } return _imageArray; } - (DRBottomShareView *)bottomShareView { if (!_bottomShareView) { _bottomShareView = [[DRBottomShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/4)]; _bottomShareView.delegate = self; } return _bottomShareView; } - (DRShareDesView *)shareDesView { if (!_shareDesView) { _shareDesView = [[DRShareDesView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40) commissPrice:self.goodModel.super_commission]; } return _shareDesView; } @end