// // MopShareView.m // mop // // Created by 王兆耀 on 2023/1/2. // #import "MopShareView.h" #import "MopPlugin.h" #import "MOPTools.h" #import #import //获取安全区域距离 #define kFinoSafeAreaTop kFinoWindowSafeAreaInset.top #define kFinoSafeAreaBottom kFinoWindowSafeAreaInset.bottom #define kFinoWindowSafeAreaInset \ ({\ UIEdgeInsets returnInsets = UIEdgeInsetsMake([UIApplication sharedApplication].statusBarFrame.size.height, 0, 0, 0);\ UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;\ if ([keyWindow respondsToSelector:NSSelectorFromString(@"safeAreaInsets")]) {\ UIEdgeInsets inset = [[keyWindow valueForKeyPath:@"safeAreaInsets"] UIEdgeInsetsValue];\ if (inset.top < [UIApplication sharedApplication].statusBarFrame.size.height) {\ inset.top = [UIApplication sharedApplication].statusBarFrame.size.height;\ }\ returnInsets = inset;\ }\ (returnInsets);\ })\ @interface MopShareView () @property (nonatomic, strong) UIView *shareView; @property (nonatomic, strong) UIImageView *appletImageView; @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UIButton *cancelButton; @property(nonatomic, strong) UICollectionView *collectionView; @property(nonatomic, strong) NSArray *dataArray; @property (nonatomic, strong) UIImageView *qrCodeImageView; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *descLabel; @end @implementation MopShareView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.dataArray = @[@{@"lightImage":@"share_wechat", @"title":@"微信好友", @"type":@"wechat"}, @{@"lightImage":@"share_moments",@"title":@"朋友圈", @"type":@"moments"}, @{@"lightImage":@"share_link",@"title":@"复制链接", @"type":@"links"}]; self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f]; float proportionWithNumber = self.frame.size.width / 375; float proportionheightNumber = self.frame.size.height / 667; self.shareView = [[UIView alloc] initWithFrame:CGRectMake(52.5 * proportionWithNumber, 46 * proportionheightNumber + kFinoSafeAreaTop, 270 * proportionWithNumber, 380 * proportionheightNumber)]; self.shareView.layer.cornerRadius = 6; self.shareView.backgroundColor = UIColor.whiteColor; UIImageView *appletImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 6, self.shareView.frame.size.width, 300 * proportionheightNumber)]; self.appletImageView = appletImageView; appletImageView.contentMode = UIViewContentModeScaleAspectFit; [self.shareView addSubview:appletImageView]; UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(226 * proportionWithNumber, 12 * proportionheightNumber, 36, 36)]; [saveButton addTarget:self action:@selector(saveOnClick) forControlEvents:UIControlEventTouchUpInside]; [saveButton setImage:[UIImage imageNamed:@"share_download"] forState:UIControlStateNormal]; [self.shareView addSubview:saveButton]; float bottomY = appletImageView.frame.size.height + appletImageView.frame.origin.y; UILabel *descLabel = [[UILabel alloc] init]; descLabel.frame = CGRectMake(15, bottomY + 12, 168, 21); descLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15]; descLabel.textColor = [MOPTools fat_dynamicColorWithLightHexString:@"#222222" darkHexString:@"#222222"]; self.titleLabel = descLabel; [self.shareView addSubview:descLabel]; UILabel *detailLabel = [[UILabel alloc] init]; detailLabel.frame = CGRectMake(15, bottomY + 26, 168, 44); detailLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11]; detailLabel.numberOfLines = 0; detailLabel.textColor = [MOPTools fat_dynamicColorWithLightHexString:@"#666666" darkHexString:@"#666666"]; self.descLabel = detailLabel; [self.shareView addSubview:detailLabel]; self.qrCodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(196, bottomY + 8, 64, 64)]; [self.shareView addSubview:self.qrCodeImageView]; [self addSubview:self.shareView]; self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.shareView.frame.origin.y + self.shareView.frame.size.height + 28 , self.frame.size.width, (221 + kFinoSafeAreaBottom))]; self.contentView.backgroundColor = [MOPTools fat_dynamicColorWithLightHexString:@"#F0F0F0" darkHexString:@"#1A1A1A"]; [self addSubview:self.contentView]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 58 )]; titleLabel.text = @"分享至"; titleLabel.textColor = [MOPTools fat_dynamicColorWithLightHexString:@"#222222" darkHexString:@"#D0D0D0"]; titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16]; titleLabel.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:titleLabel]; UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 58 , self.contentView.frame.size.width, 0.5)]; line1.backgroundColor = [MOPTools fat_dynamicColorWithLightHexString:@"#E0E0E0" darkHexString:@"#2E2E2E"]; [self.contentView addSubview:line1]; self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; NSString *cancel = [MOPTools fat_currentLanguageIsEn] ? @"Cancel" : @"取消"; self.cancelButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:17]; [self.cancelButton setTitle:cancel forState:UIControlStateNormal]; [self.cancelButton setTitleColor:[MOPTools fat_dynamicColorWithLightHexString:@"#222222" darkHexString:@"#D0D0D0"] forState:UIControlStateNormal]; [self.contentView addSubview:self.cancelButton]; self.cancelButton.frame = CGRectMake(0, self.contentView.frame.size.height - kFinoSafeAreaBottom - 56, self.contentView.frame.size.width, 56 ); [self.cancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:self.collectionView]; self.collectionView.frame = CGRectMake(0, 58.5, frame.size.width, 107); UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 165.5, self.collectionView.frame.size.width, 0.5)]; line2.backgroundColor = [MOPTools fat_dynamicColorWithLightHexString:@"#E0E0E0" darkHexString:@"#2E2E2E"]; [self.contentView addSubview:line2]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(6.0, 6.0)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.contentView.bounds; maskLayer.path = maskPath.CGPath; self.contentView.layer.mask = maskLayer; [self p_addNotifications]; } return self; } + (instancetype)viewWithData:(NSDictionary *)data { MopShareView *view = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds]; view.dataDic = data; return view; } - (void)setDataDic:(NSDictionary *)dataDic { dispatch_async(dispatch_get_main_queue(), ^{ self.qrCodeImageView.image = [MOPTools makeQRCodeForString:dataDic[@"shareUrl"]]; }); self.titleLabel.text = dataDic[@"miniAppName"]; self.descLabel.text = dataDic[@"miniAppDesc"]; } - (void)saveOnClick { // 把view生成图片并保存 UIImage *image = [MOPTools snapshotWithView:self.shareView]; UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if (!error) { // 出错 [self fatMakeToast:@"保存成功" duration:1.5 position:CSToastPositionCenter]; } else { [self fatMakeToast:[NSString stringWithFormat:@"保存失败,%@", error.description] duration:1.5 position:CSToastPositionCenter]; } } - (void)p_addNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil]; } - (void)deviceOrientationDidChange { CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; CGFloat currentWidth = self.frame.size.width; if (screenWidth != currentWidth) { [self removeFromSuperview]; } } - (void)show { self.appletImageView.image = self.image; if (self.superview) { return; } [UIView animateWithDuration:0.3 animations:^{ UIWindow * window = [UIApplication sharedApplication].keyWindow; [window addSubview:self]; } completion:^(BOOL finished) { }]; } - (void)dismiss { [UIView animateWithDuration:0.25 animations:^{ self.contentView.transform = CGAffineTransformMakeTranslation(0, self.contentView.frame.size.height); self.alpha = 0.5f; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // [self dismiss]; } #pragma mark colletionview ------------------------------ static NSString *cellID = @"cellid"; - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { MOPshareBottomViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; // cell.type = [self.dataArray[indexPath.row] intValue]; cell.imageView.image = [UIImage imageNamed:self.dataArray[indexPath.row][@"lightImage"]]; cell.label.text = self.dataArray[indexPath.row][@"title"]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.didSelcetTypeBlock) { NSString *typeString = self.dataArray[indexPath.row][@"type"]; self.didSelcetTypeBlock(typeString); } [self dismiss]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataArray.count; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(66, self.collectionView.frame.size.height); } - (UICollectionViewFlowLayout *)layout { UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init]; collectionViewLayout.minimumLineSpacing = 0; collectionViewLayout.minimumInteritemSpacing = 0; collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; return collectionViewLayout; } - (UICollectionView *)collectionView { if (!_collectionView) { _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[self layout]]; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[MOPshareBottomViewCell class] forCellWithReuseIdentifier:cellID]; _collectionView.pagingEnabled = YES; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.backgroundColor = [MOPTools fat_dynamicColorWithLightHexString:@"#F0F0F0" darkHexString:@"#1A1A1A"]; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _collectionView;; } @end @implementation MOPshareBottomViewCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [MOPTools fat_dynamicColorWithLightHexString:@"#F0F0F0" darkHexString:@"#1A1A1A"]; UIView *imageBgView = [[UIView alloc] initWithFrame:CGRectMake(18, 20, 48, 48)]; imageBgView.backgroundColor = [MOPTools fat_dynamicColorWithLightHexString:@"#FFFFFF " darkHexString:@"#2C2C2C"]; [self.contentView addSubview:imageBgView]; self.imageView = [UIImageView new]; self.imageView.frame = CGRectMake(9, 9, 30, 30); [imageBgView addSubview:self.imageView]; // self.label = [[UILabel alloc] initWithFrame:CGRectMake((frame.size.width-50)/2, 65, frame.size.width, 30)]; self.label = [[UILabel alloc] initWithFrame:CGRectMake(18, 75, 48, 12)]; self.label.font = [UIFont systemFontOfSize:10]; self.label.textColor = [UIColor grayColor]; self.label.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:self.label]; } return self; } @end