//
|
// PBDocShareView.m
|
// IphoneBIMe
|
//
|
// Created by zjf on 2018/10/30.
|
// Copyright © 2018年 ProBIM. All rights reserved.
|
//
|
|
#import "PBDocShareView.h"
|
#import "PBShareBtn.h"
|
#import "PBShareCell.h"
|
#import "PBNetworkModel.h"
|
#define ALineCount 4
|
static NSString *const cellID = @"cellID";
|
@interface PBDocShareView()<UICollectionViewDelegate, UICollectionViewDataSource>
|
@property (nonatomic, strong) UIView *bgView;
|
@property (nonatomic, strong) UICollectionView *collectionView;
|
@property (nonatomic, strong) NSMutableArray *dataArrM;
|
@end
|
@implementation PBDocShareView
|
- (instancetype)initWithFrame:(CGRect)frame {
|
if (self = [super initWithFrame:frame]) {
|
self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0];
|
[self addSubview:self.bgView];
|
[self setupUI];
|
}
|
return self;
|
}
|
|
- (void)setupUI {
|
[self.bgView addSubview:self.collectionView];
|
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(self.bgView);
|
make.left.right.equalTo(self.bgView);
|
make.height.equalTo(@144);
|
}];
|
|
UIButton *cancelBtn = [UIButton z_textButton:@"取消" fontSize:20.0 normalColor:TitleColor];
|
cancelBtn.backgroundColor = [UIColor whiteColor];
|
[cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
|
[self.bgView addSubview:cancelBtn];
|
[cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(self.collectionView.mas_bottom);
|
make.left.right.equalTo(self.bgView);
|
make.height.equalTo(@49);
|
}];
|
}
|
- (void)cancelBtnAction {
|
[self hidden];
|
}
|
|
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
CGPoint point=[[touches anyObject]locationInView:self];
|
CALayer *layer=[self.layer hitTest:point];
|
if (layer ==self.layer) {
|
[self hidden];
|
}
|
}
|
- (void)hidden {
|
[UIView animateWithDuration:0.3 animations:^{
|
self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0];
|
self.bgView.y = PBScreenHeight;
|
}completion:^(BOOL finished) {
|
for (UIView *cover in PBKeyWindow.subviews) {
|
if ([cover isKindOfClass:[PBDocShareView class]]) {
|
[cover removeFromSuperview];
|
}
|
}
|
}];
|
}
|
- (void)show {
|
[UIView animateWithDuration:0.3 animations:^{
|
self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
|
self.bgView.y = PBScreenHeight - self.visualViewHeight;
|
}];
|
}
|
#pragma mark - 懒加载
|
- (UIView *)bgView {
|
if (_bgView == nil) {
|
_bgView = [[UIView alloc]initWithFrame:CGRectMake(0, PBScreenHeight, PBScreenWidth, PBScreenHeight)];
|
_bgView.backgroundColor = PBColor(242, 242, 242);
|
}
|
return _bgView;
|
}
|
|
- (UICollectionView *)collectionView {
|
if (!_collectionView) {
|
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init];
|
CGFloat itemW = PBScreenWidth / ALineCount;
|
layout.itemSize = CGSizeMake(itemW, 144);
|
layout.minimumLineSpacing = 0;
|
layout.minimumInteritemSpacing = 0;
|
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
|
_collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
|
[_collectionView registerClass:[PBShareCell class] forCellWithReuseIdentifier:cellID];
|
_collectionView.delegate = self;
|
_collectionView.dataSource = self;
|
_collectionView.backgroundColor = PBColor(242, 242, 242);
|
}
|
return _collectionView;
|
|
}
|
- (NSMutableArray *)dataArrM {
|
if (_dataArrM == nil) {
|
_dataArrM = [[NSMutableArray alloc] init];
|
NSArray *titleArr = @[@"腾讯QQ", @"微信", @"朋友圈"];
|
NSArray *imageArr = @[@"Share_qq", @"Share_wechat", @"Share_moments"];
|
NSArray *numArr = @[@"QQ", @"WeChat", @"Moments"];
|
NSURL * qq_app_url = [NSURL URLWithString:@"mqq://"];
|
BOOL qqAli = [[UIApplication sharedApplication] canOpenURL:qq_app_url];
|
if (qqAli) {
|
NSDictionary *dict = @{
|
@"title" : titleArr[0],
|
@"image" : [UIImage imageNamed:imageArr[0]],
|
@"num" : numArr[0]
|
};
|
[_dataArrM addObject:dict];
|
}
|
NSURL * wechat_app_url = [NSURL URLWithString:@"weixin://"];
|
BOOL wechatAli = [[UIApplication sharedApplication] canOpenURL:wechat_app_url];
|
if (wechatAli) {
|
NSDictionary *dict = @{
|
@"title" : titleArr[1],
|
@"image" : [UIImage imageNamed:imageArr[1]],
|
@"num" : numArr[1]
|
};
|
NSDictionary *dict1 = @{
|
@"title" : titleArr[2],
|
@"image" : [UIImage imageNamed:imageArr[2]],
|
@"num" : numArr[2]
|
};
|
[_dataArrM addObject:dict];
|
[_dataArrM addObject:dict1];
|
}
|
}
|
return _dataArrM;
|
}
|
- (void)setVisualViewHeight:(NSInteger)visualViewHeight {
|
_visualViewHeight = visualViewHeight;
|
}
|
- (void)setTitle:(NSString *)title {
|
_title = title;
|
}
|
- (void)setDesc:(NSString *)desc {
|
_desc = desc;
|
}
|
- (void)setImage:(UIImage *)image {
|
_image = image;
|
}
|
- (void)setUrl:(NSString *)url {
|
_url = url;
|
}
|
#pragma mark - UICollectionViewDataSource
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
|
return self.dataArrM.count;
|
}
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
|
PBShareCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
|
cell.dict = self.dataArrM[indexPath.row];
|
return cell;
|
}
|
#pragma mark - UICollectionViewDelegate
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
[self hidden];
|
NSDictionary *dict = self.dataArrM[indexPath.row];
|
if ([[dict valueForKey:@"num"] isEqualToString:@"QQ"]) {
|
[self shareQQ];
|
}else if ([[dict valueForKey:@"num"] isEqualToString:@"WeChat"]) {
|
[self shareWeChat];
|
}else if ([[dict valueForKey:@"num"] isEqualToString:@"Moments"]) {
|
[self shareMoments];
|
}else {
|
|
}
|
}
|
//分享到 QQ
|
- (void)shareQQ {
|
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.title descr:self.desc thumImage:self.image];
|
//设置网页地址
|
shareObject.webpageUrl = self.url;
|
//创建分享消息对象
|
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
|
//分享消息对象设置分享内容对象
|
messageObject.shareObject = shareObject;
|
/**
|
设置分享
|
@param data 分享返回信息
|
@param error 失败信息
|
@param UMSocialPlatformType 分享平台
|
*/
|
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self.viewController completion:^(id data, NSError *error) {
|
if (error) {
|
UMSocialLogInfo(@"************Share fail with error %@*********",error);
|
}else{
|
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
|
UMSocialShareResponse *resp = data;
|
//分享结果消息
|
UMSocialLogInfo(@"response message is %@",resp.message);
|
//第三方原始返回的数据
|
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
|
}else{
|
UMSocialLogInfo(@"response data is %@",data);
|
}
|
}
|
}];
|
}
|
|
//分享到微信
|
- (void)shareWeChat {
|
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.title descr:self.desc thumImage:self.image];
|
//设置网页地址
|
shareObject.webpageUrl = self.url;
|
//创建分享消息对象
|
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
|
//分享消息对象设置分享内容对象
|
messageObject.shareObject = shareObject;
|
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self.viewController completion:^(id data, NSError *error) {
|
if (error) {
|
UMSocialLogInfo(@"************Share fail with error %@*********",error);
|
}else{
|
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
|
UMSocialShareResponse *resp = data;
|
//分享结果消息
|
UMSocialLogInfo(@"response message is %@",resp.message);
|
//第三方原始返回的数据
|
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
|
|
}else{
|
UMSocialLogInfo(@"response data is %@",data);
|
}
|
}
|
|
}];
|
|
}
|
|
//分享到朋友圈
|
- (void)shareMoments {
|
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.title descr:self.desc thumImage:self.image];
|
//设置网页地址
|
shareObject.webpageUrl = self.url;
|
//创建分享消息对象
|
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
|
//分享消息对象设置分享内容对象
|
messageObject.shareObject = shareObject;
|
/**
|
设置分享
|
|
@param data 分享返回信息
|
@param error 失败信息
|
@param UMSocialPlatformType 分享平台
|
*/
|
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine messageObject:messageObject currentViewController:self.viewController completion:^(id data, NSError *error) {
|
if (error) {
|
UMSocialLogInfo(@"************Share fail with error %@*********",error);
|
}else{
|
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
|
UMSocialShareResponse *resp = data;
|
//分享结果消息
|
UMSocialLogInfo(@"response message is %@",resp.message);
|
//第三方原始返回的数据
|
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
|
|
}else{
|
UMSocialLogInfo(@"response data is %@",data);
|
}
|
}
|
}];
|
}
|
/*
|
// Only override drawRect: if you perform custom drawing.
|
// An empty implementation adversely affects performance during animation.
|
- (void)drawRect:(CGRect)rect {
|
// Drawing code
|
}
|
*/
|
|
@end
|