zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
//  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