// // YMInvitingPrizesSharePosterPopupView.m // MSYOUPAI // // Created by YoMi on 2024/3/15. // Copyright © 2024 MS. All rights reserved. // #import "YMInvitingPrizesSharePosterPopupView.h" #import "YMInvitingPrizesViewModel.h" @interface YMInvitingPrizesSharePosterPopupView () /// 邀请有奖VM @property (nonatomic, strong) YMInvitingPrizesViewModel *viewModel; /// 海报图 @property (nonatomic, strong) TYCyclePagerView *posterView; /// 按钮视图 @property (nonatomic, strong) UIView *buttonView; /// 复制链接按钮 @property (nonatomic, strong) UIButton *copyLinkBtn; /// 切换模式按钮 @property (nonatomic, strong) UIButton *switchModeBtn; /// 保存二维码按钮 @property (nonatomic, strong) UIButton *saveQRCodeBtn; /// 是否反方向 @property (nonatomic, assign) BOOL isReverseDirectionScroll; @end @implementation YMInvitingPrizesSharePosterPopupView - (void)ym_setupViews{ self.frame = CGRectMake(0, 0, kFrameWidth, kFrameHeight - kYMNavHeight); [self addSubview:self.posterView]; [self addSubview:self.buttonView]; [self.buttonView addSubview:self.copyLinkBtn]; [self.buttonView addSubview:self.switchModeBtn]; [self.buttonView addSubview:self.saveQRCodeBtn]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints{ [self.posterView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self); make.left.equalTo(self); make.right.equalTo(self); }]; [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.posterView.mas_bottom).offset(adapt(40)); make.left.equalTo(self); make.right.equalTo(self); make.bottom.equalTo(self); }]; [self.copyLinkBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.switchModeBtn.mas_centerY); make.right.equalTo(self.switchModeBtn.mas_left).offset(adapt(-15)); make.size.mas_equalTo(CGSizeMake(adapt(120), adapt(120))); }]; [self.switchModeBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.buttonView.mas_centerX); make.top.equalTo(self.buttonView).offset(adapt(20)); make.bottom.equalTo(self.buttonView).offset(adapt(-20)); make.size.mas_equalTo(CGSizeMake(adapt(120), adapt(120))); }]; [self.saveQRCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.switchModeBtn.mas_centerY); make.left.equalTo(self.switchModeBtn.mas_right).offset(adapt(15)); make.size.mas_equalTo(CGSizeMake(adapt(120), adapt(120))); }]; // NSArray *btnArr = @[self.copyLinkBtn,self.switchModeBtn,self.saveQRCodeBtn]; // // // 实现masonry水平固定控件宽度方法 // // axisType 横排还是竖排 // // fixedSpacing 两个控件间隔 // // leadSpacing 第一个控件与边缘的间隔 // // tailSpacing 最后一个控件与边缘的间隔 // [btnArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:adapt(15) leadSpacing:adapt(10) tailSpacing:adapt(10)]; // // // 设置array的垂直方向的约束 // [btnArr mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(self.buttonView).offset(adapt(20)); // make.bottom.equalTo(self.buttonView).offset(adapt(-20)); // make.height.mas_equalTo(adapt(120)); // }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMInvitingPrizesViewModel *)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; @weakify(self); [[self.viewModel.refreshUISubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id result) { @strongify(self) [self.posterView reloadData]; }]; } #pragma mark - TYCyclePagerViewDataSource - (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView { return self.viewModel.posterDataArray.count; } - (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index { YMInvitingPrizesSharePosterCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([YMInvitingPrizesSharePosterCell class]) forIndex:index]; [cell ym_bindViewModel:self.viewModel.posterDataArray[index]]; return cell; } /** banner样式*/ - (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView { TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init]; layout.itemSize = CGSizeMake(adapt(285), adapt(393)); layout.itemSpacing = adapt(50); layout.itemHorizontalCenter = YES; layout.scrollDirection = TYCyclePagerScrollDirectionHorizontal; return layout; } #pragma mark - TYCyclePagerViewDelegate /** pagerView当前展示*/ - (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex { if (toIndex == 0) { self.isReverseDirectionScroll = NO; } if (toIndex+1 == self.viewModel.posterDataArray.count) { self.isReverseDirectionScroll = YES; } } - (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index{ } - (void)pagerViewDidScroll:(TYCyclePagerView *)pageView{ } /** pagerView将开始拖动*/ - (void)pagerViewWillBeginDragging:(TYCyclePagerView *)pageView{ } /** pagerView将开始减速*/ - (void)pagerViewWillBeginDecelerating:(TYCyclePagerView *)pageView{ } /** pagerView确认结束*/ - (void)pagerViewDidEndDragging:(TYCyclePagerView *)pageView willDecelerate:(BOOL)decelerate{ } /** pagerView确认减速*/ - (void)pagerViewDidEndDecelerating:(TYCyclePagerView *)pageView{ } /** pagerView将开始滚动动画*/ - (void)pagerViewWillBeginScrollingAnimation:(TYCyclePagerView *)pageView{ } /** pagerView确认结束滚动动画*/ - (void)pagerViewDidEndScrollingAnimation:(TYCyclePagerView *)pageView{ } - (void)switchModeScrollToItem{ NSInteger currentIndex = self.posterView.curIndex; if (self.isReverseDirectionScroll) { currentIndex--; [self.posterView scrollToItemAtIndex:currentIndex animate:YES]; } else { currentIndex++; [self.posterView scrollToItemAtIndex:currentIndex animate:YES]; } } #pragma mark - 保存到相册 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if(error) { [ZCHUDHelper showTitle:@"保存图片失败"]; }else { [ZCHUDHelper showTitle:@"保存图片成功"]; } } - (UIImage *)getCurrentImage { UIImage *image = nil; YMInvitingPrizesSharePosterCell *cell = (YMInvitingPrizesSharePosterCell *)[self.posterView curIndexCell]; if (cell.finishedLoad) { image = [cell cutImage:cell.baseView]; } else { [ZCHUDHelper showTitle:@"请等待图片加载完成"]; } return image; } - (TYCyclePagerView *)posterView{ if (!_posterView) { _posterView = [[TYCyclePagerView alloc]init]; _posterView.dataSource = self; _posterView.delegate = self; //无限滚动 _posterView.isInfiniteLoop = NO; //自动轮播 _posterView.autoScrollInterval = 0; [_posterView registerClass:[YMInvitingPrizesSharePosterCell class] forCellWithReuseIdentifier:NSStringFromClass([YMInvitingPrizesSharePosterCell class])]; _posterView.userInteractionEnabled = YES; WS(weakSelf) UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; [_posterView addGestureRecognizer:tap]; [[[tap rac_gestureSignal] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) { if (weakSelf.dismissBlock) { weakSelf.dismissBlock(); } }]; } return _posterView; } - (UIView *)buttonView{ if (!_buttonView) { _buttonView = [[UIView alloc]init]; _buttonView.backgroundColor = HexColorFromRGB(0xFFFFFF); [_buttonView addRectCorner:UIRectCornerTopLeft|UIRectCornerTopRight radius:adapt(10)]; _buttonView.userInteractionEnabled = YES; WS(weakSelf) UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init]; [_buttonView addGestureRecognizer:tap]; [[[tap rac_gestureSignal] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) { if (weakSelf.dismissBlock) { weakSelf.dismissBlock(); } }]; } return _buttonView; } - (UIButton *)copyLinkBtn { if(!_copyLinkBtn){ _copyLinkBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _copyLinkBtn.frame = CGRectMake(0,0,adapt(120),adapt(120)); _copyLinkBtn.titleLabel.font = LCFont(12); [_copyLinkBtn setTitleColor:HexColorFromRGB(0x333333) forState:UIControlStateNormal]; [_copyLinkBtn ym_imageLocationStyle:YMImageLocationStyleTop spacing:adapt(5) imageLocationBlock:^(UIButton * _Nonnull button) { [button setTitle:@"复制链接" forState:UIControlStateNormal]; [button setImage:ImageByName(@"ym_inviting_prizes_share_poster_copy_link_icon") forState:UIControlStateNormal]; }]; WS(weakSelf) [[[_copyLinkBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) { UIPasteboard *pab = [UIPasteboard generalPasteboard]; pab.string = weakSelf.viewModel.posterDataArray[0].qrCodeContent; if (pab == nil) { [ZCHUDHelper showTitle:@"复制失败"]; }else{ [ZCHUDHelper showTitle:@"已复制"]; } }]; } return _copyLinkBtn; } - (UIButton *)switchModeBtn { if(!_switchModeBtn){ _switchModeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _switchModeBtn.frame = CGRectMake(0,0,adapt(120),adapt(120)); _switchModeBtn.titleLabel.font = LCFont(12); [_switchModeBtn setTitleColor:HexColorFromRGB(0x333333) forState:UIControlStateNormal]; [_switchModeBtn ym_imageLocationStyle:YMImageLocationStyleTop spacing:adapt(5) imageLocationBlock:^(UIButton * _Nonnull button) { [button setTitle:@"切换模式" forState:UIControlStateNormal]; [button setImage:ImageByName(@"ym_inviting_prizes_share_poster_switch_mode_icon") forState:UIControlStateNormal]; }]; WS(weakSelf) [[[_switchModeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) { [weakSelf switchModeScrollToItem]; }]; } return _switchModeBtn; } - (UIButton *)saveQRCodeBtn { if(!_saveQRCodeBtn){ _saveQRCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _saveQRCodeBtn.frame = CGRectMake(0,0,adapt(120),adapt(120)); _saveQRCodeBtn.titleLabel.font = LCFont(12); [_saveQRCodeBtn setTitleColor:HexColorFromRGB(0x333333) forState:UIControlStateNormal]; [_saveQRCodeBtn ym_imageLocationStyle:YMImageLocationStyleTop spacing:adapt(5) imageLocationBlock:^(UIButton * _Nonnull button) { [button setTitle:@"保存二维码" forState:UIControlStateNormal]; [button setImage:ImageByName(@"ym_inviting_prizes_share_poster_save_qr_code_icon") forState:UIControlStateNormal]; }]; WS(weakSelf) [[[_saveQRCodeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) { UIImage *poster = [weakSelf getCurrentImage]; if (poster == nil) { return; } NSData *imageData = UIImagePNGRepresentation(poster); UIImage *image = [UIImage imageWithData:imageData]; UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); }]; } return _saveQRCodeBtn; } @end @interface YMInvitingPrizesSharePosterCell() /** 海报lVM*/ @property (nonatomic, strong) YMInvitingPrizesSharePosterCellViewModel *viewModel; /** 完成加载*/ @property (nonatomic, assign, readwrite) BOOL finishedLoad; /** 海报图片*/ @property (nonatomic, strong) UIImageView *posterImage; /** 用户头像图片*/ @property (nonatomic, strong) UIImageView *userAvatarImage; /** 用户昵称标签*/ @property (nonatomic, strong) UILabel *userNicknameLb; /** 用户代码标签*/ @property (nonatomic, strong) UILabel *userCodeLb; /** 二维码图片*/ @property (nonatomic, strong) UIImageView *qrCodeImage; @end @implementation YMInvitingPrizesSharePosterCell - (void)ym_setupViews{ [self.contentView addSubview:self.baseView]; [self.baseView addSubview:self.posterImage]; [self.baseView addSubview:self.userAvatarImage]; [self.baseView addSubview:self.userNicknameLb]; [self.baseView addSubview:self.userCodeLb]; [self.baseView addSubview:self.qrCodeImage]; [self setNeedsUpdateConstraints]; [self updateConstraintsIfNeeded]; } - (void)updateConstraints { [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView); make.left.equalTo(self.contentView); make.right.equalTo(self.contentView); make.bottom.equalTo(self.contentView); }]; [self.posterImage mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.baseView); make.left.equalTo(self.baseView); make.right.equalTo(self.baseView); }]; [self.userAvatarImage mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.posterImage.mas_bottom).offset(adapt(30)); make.left.equalTo(self.baseView).offset(adapt(30)); make.width.height.mas_equalTo(adapt(60)); make.bottom.equalTo(self.baseView).offset(adapt(-30)); }]; [self.userNicknameLb mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.userAvatarImage.mas_centerY).offset(adapt(-5)); make.left.equalTo(self.userAvatarImage.mas_right).offset(adapt(10)); }]; [self.userCodeLb mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.userAvatarImage.mas_centerY).offset(adapt(5)); make.left.equalTo(self.userAvatarImage.mas_right).offset(adapt(10)); }]; [self.qrCodeImage mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.userAvatarImage).offset(adapt(0)); make.right.equalTo(self.baseView).offset(adapt(-20)); make.width.height.mas_equalTo(adapt(46)); }]; [super updateConstraints]; } - (void)ym_bindViewModel:(YMInvitingPrizesSharePosterCellViewModel*)viewModel{ if (!viewModel) { return; } _viewModel = viewModel; // [self.posterImage sd_setImageWithURL:[LCTools getImageUrlWithAddress:@""] placeholderImage:nil options:SDWebImageRetryFailed | SDWebImageHighPriority | SDWebImageProgressiveDownload | SDWebImageAvoidAutoSetImage | SDWebImageScaleDownLargeImages completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { // self.finishedLoad = image ? YES : NO; // if (image && cacheType == SDImageCacheTypeNone) { // CATransition *transition = [CATransition animation]; // transition.type = kCATransitionFade; // transition.duration = 0.3; // transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; // [self.posterImage.layer addAnimation:transition forKey:nil]; // } // }]; self.finishedLoad = YES; self.posterImage.image = ImageByName(self.viewModel.posterUrl); //[self.userAvatarImage sd_setImageWithURL:[LCTools getImageUrlWithAddress:[LCSaveModel getUserModel].youpaipuserinfo.youpaipavatar]]; self.userAvatarImage.image = [self createQRCodeWithData:self.viewModel.qrCodeContent size:adapt(120)]; self.userNicknameLb.text = [LCSaveModel getUserModel].youpaipuserinfo.youpaipnickname; self.userCodeLb.text = stringFormat(@"用户号:%@",[LCSaveModel getUserModel].youpaipuserinfo.youpaipusercode); //UIImage *qrCode = [self createQRCodeWithData:self.viewModel.qrCodeContent size:adapt(100)]; self.qrCodeImage.image = ImageByName(@"ym_inviting_prizes_share_app_icon"); } - (UIImage *)cutImage { UIImage *imageRet = nil; int scale = [UIScreen mainScreen].scale; CGSize Ksize = CGSizeMake(self.posterImage.frame.size.width*scale, self.posterImage.frame.size.height*scale); // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃 if (CGSizeEqualToSize(Ksize, CGSizeZero)) { return nil; } UIGraphicsBeginImageContextWithOptions(Ksize, NO, scale); [self.posterImage.image drawInRect:CGRectMake(0,0, Ksize.width, Ksize.height)]; imageRet = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageRet; } - (UIImage *)cutImage:(UIView *)view { int scale = [UIScreen mainScreen].scale; CGSize shotSize = CGSizeMake(view.frame.size.width*scale, view.frame.size.height*scale); // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃 if (CGSizeEqualToSize(shotSize, CGSizeZero)) { return nil; } UIGraphicsBeginImageContextWithOptions(shotSize, NO, scale); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = tempImage.CGImage; CGRect rect = CGRectMake(0,0, shotSize.width, shotSize.height); CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect); UIImage *image = [[UIImage alloc]initWithCGImage:imageRefRect]; return image; } - (UIImage *)createQRCodeWithData:(NSString *)data size:(CGFloat)size{ NSData *string_data = [data dataUsingEncoding:NSUTF8StringEncoding]; // 1、二维码滤镜 CIFilter *fileter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [fileter setValue:string_data forKey:@"inputMessage"]; /* L (低):可以纠正7%左右的错误 M (中):可以纠正15%左右的错误 Q (高):可以纠正25%左右的错误 H (高):可以纠正30%左右的错误 */ [fileter setValue:@"H" forKey:@"inputCorrectionLevel"]; CIImage *ciImage = fileter.outputImage; // 2、颜色滤镜 CIFilter *color_filter = [CIFilter filterWithName:@"CIFalseColor"]; [color_filter setValue:ciImage forKey:@"inputImage"]; [color_filter setValue:[CIColor colorWithCGColor:HexColorFromRGB(0x000000).CGColor] forKey:@"inputColor0"]; [color_filter setValue:[CIColor colorWithCGColor:HexColorFromRGB(0xFFFFFF).CGColor] forKey:@"inputColor1"]; // 3、生成处理 CIImage *outImage = color_filter.outputImage; CGFloat scale = size / outImage.extent.size.width; outImage = [outImage imageByApplyingTransform:CGAffineTransformMakeScale(scale, scale)]; return [UIImage imageWithCIImage:outImage]; } - (UIView *)baseView{ if (!_baseView) { _baseView = [[UIView alloc]init]; _baseView.backgroundColor = HexColorFromRGB(0xFFFFFF); [_baseView addRectCorner:UIRectCornerAllCorners radius:adapt(16)]; } return _baseView; } - (UIImageView *)posterImage{ if (!_posterImage) { _posterImage = [[UIImageView alloc]init]; //_posterImage.contentMode = UIViewContentModeScaleAspectFit; } return _posterImage; } - (UIImageView *)userAvatarImage{ if (!_userAvatarImage) { _userAvatarImage = [[UIImageView alloc]init]; _userAvatarImage.contentMode = UIViewContentModeScaleAspectFit; } return _userAvatarImage; } - (UILabel *)userNicknameLb{ if (!_userNicknameLb) { _userNicknameLb = [[UILabel alloc]init]; _userNicknameLb.font = LCBoldFont(13); _userNicknameLb.textColor = HexColorFromRGB(0x333333); _userNicknameLb.textAlignment = NSTextAlignmentLeft; _userNicknameLb.text = @"*****"; /** *抗拉伸 setContentHuggingPriority(值越高,越不容易拉伸) */ [_userNicknameLb setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal]; /** *抗压缩 setContentCompressionResistancePriority(值越高,越不容易压缩) **/ [_userNicknameLb setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal]; } return _userNicknameLb; } - (UILabel *)userCodeLb{ if (!_userCodeLb) { _userCodeLb = [[UILabel alloc]init]; _userCodeLb.font = LCFont(12); _userCodeLb.textColor = HexColorFromRGB(0x666666); _userCodeLb.textAlignment = NSTextAlignmentLeft; _userCodeLb.text = @"*****"; /** *抗拉伸 setContentHuggingPriority(值越高,越不容易拉伸) */ [_userCodeLb setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal]; /** *抗压缩 setContentCompressionResistancePriority(值越高,越不容易压缩) **/ [_userCodeLb setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal]; } return _userCodeLb; } - (UIImageView *)qrCodeImage{ if (!_qrCodeImage) { _qrCodeImage = [[UIImageView alloc]init]; } return _qrCodeImage; } @end