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
//
//  PBExamineTableHeaderView.m
//  IphoneBIMe
//
//  Created by zjf on 2018/11/29.
//  Copyright © 2018 ProBIM. All rights reserved.
//
 
#import "PBExamineTableHeaderView.h"
#import "PBImageCollectionViewCell.h"
#import "PBProjectModel.h"
 
static NSString *cellID = @"cellID";
@interface PBExamineTableHeaderView()<UICollectionViewDataSource, UICollectionViewDelegate, SDPhotoBrowserDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *photoBtn;
@property (nonatomic, strong) UIPageControl *pageControl;
//记录当前页码
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, weak) UIImageView *videoImageV;
@property (nonatomic, weak) UIButton *playBtn;
@property (nonatomic, weak) UIButton *deleteBtn;
@end
 
@implementation PBExamineTableHeaderView
 
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    UIImageView *bgImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Examine_add_photo_bg"]];
    [self addSubview:bgImageV];
    [bgImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self);
    }];
    UIButton *uploadPhotoBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"Examine_upload_photo"]];
    [uploadPhotoBtn addTarget:self action:@selector(uploadPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:uploadPhotoBtn];
    [uploadPhotoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self);
    }];
    self.collectionView.hidden = YES;
    
    UIButton *photoBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"Examine_camera"]];
    [photoBtn addTarget:self action:@selector(uploadPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:photoBtn];
    [photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-10);
    }];
    photoBtn.hidden = YES;
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = 5;
    pageControl.currentPage = 3;
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    pageControl.currentPageIndicatorTintColor = WarningColor;
    pageControl.hidesForSinglePage = YES;
    [self addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-13);
    }];
    self.pageControl.currentPage = 0;
    self.photoBtn = photoBtn;
    self.pageControl = pageControl;
    self.pageControl.hidden = YES;
    
    UIImageView *videoImageV = [[UIImageView alloc] init];
    [self addSubview:videoImageV];
    [videoImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self);
    }];
    UIButton *playBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"details_play"]];
    [playBtn addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:playBtn];
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(92, 92));
    }];
    UIButton *deleteBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"Issue_image_delete"]];
    [deleteBtn addTarget:self action:@selector(deleteVideo) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.right.equalTo(self).offset(-5);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    self.videoImageV = videoImageV;
    self.playBtn = playBtn;
    self.deleteBtn = deleteBtn;
    self.deleteBtn.hidden = YES;
    self.playBtn.hidden = YES;
    self.videoImageV.hidden = YES;
}
 
- (void)uploadPhoto {
    if (self.ChooseImageBlock) {
        self.ChooseImageBlock();
    }
}
- (void)playVideo {
    NSLog(@"开始播放");
    if (self.PlayVideoBlock) {
        self.PlayVideoBlock();
    }
}
- (void)deleteVideo {
    NSLog(@"删除视频");
    if (self.DeleteVideoBlock) {
        self.DeleteVideoBlock();
    }
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init];
        layout.itemSize = CGSizeMake(self.width, self.height);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
        [_collectionView registerClass:[PBImageCollectionViewCell class] forCellWithReuseIdentifier:cellID];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_isAddExamine) {
       return self.imageArr.count;
    }else {
       return self.detailsImageArr.count;
    }
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PBImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.roleType = self.roleType;
    cell.DeleteImageBlock = ^{
        NSInteger count;
        if (self.isAddExamine) {
            count = self.imageArr.count - 1;
        }else {
            count = self.detailsImageArr.count - 1;
        }
        if (self.currentPage == (self.imageArr.count - 1) && self.currentPage != 0) {
                --self.currentPage;
        }
        if (self.DeleteImageBlock) {
            self.DeleteImageBlock(indexPath.row);
        }
    };
    
    if (_isAddExamine) {
        cell.image = self.imageArr[indexPath.row];
    }else {
        NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl, [self.detailsImageArr[indexPath.row] valueForKey:@"AttachmentUrl"]];
        cell.imageUrl = url;
    }
    return cell;
}
 
#pragma mark - UICollectionViewDelegate
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.currentPage = scrollView.contentOffset.x / self.width;
    self.pageControl.currentPage = self.currentPage;
}
 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new];
    photoBrowser.delegate = self;
    photoBrowser.currentImageIndex = indexPath.item;
    if (self.isAddExamine) {
        photoBrowser.imageCount = self.imageArr.count;
    }else {
        photoBrowser.imageCount = self.detailsImageArr.count;
    }
    photoBrowser.sourceImagesContainerView = self.collectionView;
    [photoBrowser show];
}
 
#pragma mark  SDPhotoBrowserDelegate
// 返回临时占位图片(即原来的小图)
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    // 不建议用此种方式获取小图,这里只是为了简单实现展示而已
    PBImageCollectionViewCell *cell = (PBImageCollectionViewCell *)[self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    return cell.imageV.image;
}
 
 
- (void)setImageArr:(NSMutableArray *)imageArr {
    _imageArr = imageArr;
    self.videoImageV.hidden = YES;
    self.playBtn.hidden = YES;
    self.deleteBtn.hidden = YES;
    [self setPageControl];
}
- (void)setVideoArr:(NSMutableArray *)videoArr {
    _videoArr = videoArr;
    self.videoImageV.hidden = NO;
    self.playBtn.hidden = NO;
    self.deleteBtn.hidden = NO;
    LFResultVideo *result = videoArr[0];
    self.videoImageV.image = result.coverImage;
}
 
- (void)setPageControl {
    NSArray *arr;
    if (_isAddExamine) {
        arr = self.imageArr;
    }else {
        arr = self.detailsImageArr;
    }
    if (arr.count != 0) {
        self.collectionView.hidden = NO;
        self.photoBtn.hidden = NO;
        self.pageControl.hidden = NO;
        if (self.isAddExamine) {
            self.pageControl.numberOfPages = self.imageArr.count;
        }else {
            self.pageControl.numberOfPages = self.detailsImageArr.count;
        }
        self.pageControl.currentPage = self.currentPage;
        [self.collectionView reloadData];
    }else {
        self.photoBtn.hidden = YES;
        self.collectionView.hidden = YES;
        self.pageControl.hidden = YES;
        [self.collectionView reloadData];
    }
}
- (void)setDetailsImageArr:(NSArray *)detailsImageArr {
    _detailsImageArr = detailsImageArr;
    [self setPageControl];
}
- (void)setIsAddExamine:(BOOL)isAddExamine {
    _isAddExamine = isAddExamine;
}
- (void)setProjectModel:(PBProjectModel *)projectModel {
    _projectModel = projectModel;
}
- (void)setRoleType:(RoleType)roleType {
    _roleType = roleType;
}
@end