悟空记账

SHAddImgView.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // SHAddImgView.m
  3. // SHAddImageView
  4. //
  5. // Created by HaoSun on 2018/2/3.
  6. // Copyright © 2018年 YHKIT. All rights reserved.
  7. //
  8. #import "SHAddImgView.h"
  9. #import <AssetsLibrary/AssetsLibrary.h>
  10. #import <MobileCoreServices/UTCoreTypes.h>
  11. #import "SHAddCollectionCell.h"
  12. @interface SHAddImgView()<UICollectionViewDelegate,UICollectionViewDataSource,SHAddCollectionCellDelegate,UIImagePickerControllerDelegate>
  13. /**
  14. 承载图片的CollectionView
  15. */
  16. @property (nonatomic, strong) UICollectionView *collectionView;
  17. /**
  18. 增加图片的按钮
  19. */
  20. @property (nonatomic, weak) UIImageView *addImgView;//加号图片
  21. /**
  22. 图片数据源
  23. */
  24. @property (nonatomic, strong) NSMutableArray <SHAddImgModel *>*imageModels;
  25. /**
  26. 图片数据源
  27. */
  28. @property (nonatomic, strong) NSMutableArray <UIImage *>*images;
  29. /**
  30. 通过configmodel 获取 每个ITEM的高度 (高度 = 宽度)
  31. */
  32. @property (nonatomic, assign) CGFloat itemHeight;
  33. /**
  34. 通过itemHeight 获取 每个ImageView的高度 (高度 = itemHeight - padding )
  35. */
  36. @property (nonatomic, assign) CGFloat imageHeight;
  37. /**
  38. 一行展示数量
  39. */
  40. @property (nonatomic, assign) NSInteger maximageColumn;
  41. @end
  42. @implementation SHAddImgView
  43. - (instancetype)initWithMaxColumn:(NSInteger)maxColumn {
  44. self = [super init];
  45. if (self) {
  46. _maximageColumn = maxColumn;
  47. [self setupPhotoView];
  48. }
  49. return self;
  50. }
  51. #pragma mark - 图片的view
  52. - (void)setupPhotoView {
  53. [self addSubview:self.collectionView];
  54. UIImageView *addImgView = [[UIImageView alloc] init];
  55. addImgView.userInteractionEnabled = YES;
  56. [addImgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addImgIBAction)]];
  57. _addImgView = addImgView;
  58. [self addSubview:_addImgView];
  59. }
  60. - (void)addImgIBAction {
  61. [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
  62. __weak typeof(self) weakSelf = self;
  63. if (self.delegate && [self.delegate respondsToSelector:@selector(addImgIBActionImageSource:)]) {
  64. [self.delegate addImgIBActionImageSource:self.images];
  65. }
  66. if (self.imageModels.count >= _configModel.maxPhoto) {
  67. NSLog(@"=已经达到最大值==");
  68. return ;
  69. }
  70. UIAlertController *alertConVc = [UIAlertController alertControllerWithTitle:@"请选择获取图片的方式" message:@"选择方式" preferredStyle:UIAlertControllerStyleActionSheet];
  71. UIAlertAction *fromcPhotograph = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  72. //拍照按钮
  73. #if !TARGET_IPHONE_SIMULATOR
  74. UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
  75. ipc.view.backgroundColor = [UIColor whiteColor];
  76. ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
  77. ipc.delegate = self;
  78. [[self viewController] presentViewController:ipc animated:YES completion:nil];
  79. #endif
  80. }];
  81. UIAlertAction *fromcAlbum = [UIAlertAction actionWithTitle:@"从手机相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  82. if ([ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusNotDetermined) {
  83. ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
  84. [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
  85. if (stop) {
  86. [weakSelf setupTZImageVCNumber:_configModel.maxPhoto - self.imageModels.count];
  87. }
  88. *stop = TRUE;
  89. } failureBlock:^(NSError *error) {
  90. [weakSelf setupTZImageVCNumber:_configModel.maxPhoto - self.imageModels.count];
  91. }];
  92. }else{
  93. [weakSelf setupTZImageVCNumber:_configModel.maxPhoto - self.imageModels.count];
  94. }
  95. }];
  96. UIAlertAction *cancleButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  97. }];
  98. [alertConVc addAction:fromcPhotograph];
  99. [alertConVc addAction:fromcAlbum];
  100. [alertConVc addAction:cancleButton];
  101. [[self viewController] presentViewController:alertConVc animated:YES completion:nil];
  102. }
  103. - (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
  104. //判断手势状态
  105. switch (longGesture.state) {
  106. case UIGestureRecognizerStateBegan:{
  107. //判断手势落点位置是否在路径上
  108. NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
  109. if (indexPath == nil) {
  110. break;
  111. }
  112. //在路径上则开始移动该路径上的cell
  113. [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
  114. }
  115. break;
  116. case UIGestureRecognizerStateChanged:
  117. //移动过程当中随时更新cell位置
  118. [self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
  119. break;
  120. case UIGestureRecognizerStateEnded:
  121. //移动结束后关闭cell移动
  122. [self.collectionView endInteractiveMovement];
  123. break;
  124. default:
  125. [self.collectionView cancelInteractiveMovement];
  126. break;
  127. }
  128. }
  129. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  130. return 1;
  131. }
  132. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  133. return self.imageModels.count;
  134. }
  135. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  136. SHAddCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SHAddCollectionCell" forIndexPath:indexPath];
  137. SHAddImgModel *model = self.imageModels[indexPath.item];
  138. cell.delegate = self;
  139. cell.imageModel = model;
  140. cell.imageHeight = _imageHeight;
  141. cell.configModel = _configModel;
  142. return cell;
  143. }
  144. - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
  145. //返回YES允许其item移动
  146. return YES;
  147. }
  148. - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
  149. //取出源item数据
  150. id objc = [self.imageModels objectAtIndex:sourceIndexPath.item];
  151. //从资源数组中移除该数据
  152. [self.imageModels removeObject:objc];
  153. //将数据插入到资源数组中的目标位置上
  154. [self.imageModels insertObject:objc atIndex:destinationIndexPath.item];
  155. }
  156. - (UICollectionView *)collectionView{
  157. if (!_collectionView) {
  158. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  159. layout.minimumInteritemSpacing = 0.0;
  160. layout.minimumLineSpacing = 0.0;
  161. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  162. [_collectionView registerClass:[SHAddCollectionCell class] forCellWithReuseIdentifier:@"SHAddCollectionCell"];
  163. _collectionView.backgroundColor = [UIColor clearColor];
  164. _collectionView.dataSource = self;
  165. //此处给其增加长按手势,用此手势触发cell移动效果
  166. [_collectionView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)]];
  167. }
  168. return _collectionView;
  169. }
  170. #pragma mark - 删除内容
  171. - (void)delImageView:(SHAddImgModel *)model {
  172. [self.imageModels removeObject:model];
  173. [self.images removeObject:model.image];
  174. [self.collectionView reloadData];
  175. [self resetAddImgFrame];
  176. }
  177. #pragma mark - 进入图片浏览器
  178. - (void)imageViewTap:(SHAddImgModel *)imageModel {
  179. if (!_configModel.shouldPhotoBrowser) {
  180. return;
  181. }
  182. NSInteger itemIndex = 0;
  183. NSMutableArray *images = [NSMutableArray array];
  184. for (NSInteger index = 0; index < self.imageModels.count; index++) {
  185. SHAddImgModel *model = self.imageModels[index];
  186. if ([imageModel isEqual:model]) {
  187. itemIndex = index;
  188. }
  189. [images addObject:model.image];
  190. }
  191. if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemSource:index:)]) {
  192. [self.delegate didSelectItemSource:images index:itemIndex];
  193. }
  194. }
  195. //相册选择器
  196. - (void)setupTZImageVCNumber:(NSInteger)photoNum{
  197. }
  198. - (void)resetAddImgFrame{
  199. NSInteger imgRow = _imageModels.count % _maximageColumn;
  200. NSInteger imgLine = _imageModels.count / _maximageColumn;
  201. CGFloat starIndexX = (_itemHeight + kItemPadding) * imgRow + kItemPadding * 0.5;
  202. CGFloat starIndexY = (_itemHeight + kItemPadding) * imgLine + kItemPadding * 0.5;
  203. if(_imageModels.count < _configModel.maxPhoto && _configModel.addBtnHidden == NO){
  204. _addImgView.frame = CGRectMake(starIndexX, starIndexY, _imageHeight, _imageHeight);
  205. _addImgView.hidden = NO;
  206. if (self.delegate && [self.delegate respondsToSelector:@selector(addimageViewUpdateHeight:)]) {
  207. [self.delegate addimageViewUpdateHeight:((imgLine + 1) * (_itemHeight + kItemPadding))];
  208. }
  209. }else{
  210. _addImgView.hidden = YES;
  211. if (self.delegate && [self.delegate respondsToSelector:@selector(addimageViewUpdateHeight:)]) {
  212. [self.delegate addimageViewUpdateHeight:((imgLine + 1) * (_itemHeight + kItemPadding))];
  213. }
  214. }
  215. }
  216. /**
  217. * 返回当前视图的控制器
  218. */
  219. - (UIViewController *)viewController {
  220. for (UIView* next = [self superview]; next; next = next.superview) {
  221. UIResponder *nextResponder = [next nextResponder];
  222. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  223. return (UIViewController *)nextResponder;
  224. }
  225. }
  226. return nil;
  227. }
  228. /**
  229. 存草稿进入展示图片
  230. @param defualtImages images
  231. */
  232. - (void)setDefualtImages:(NSArray<UIImage *> *)defualtImages {
  233. if (_configModel.isDefualt) {
  234. _defualtImages = defualtImages;
  235. for (UIImage *image in defualtImages) {
  236. SHAddImgModel *model = [[SHAddImgModel alloc] init];
  237. model.image = image;
  238. [self.imageModels addObject:model];
  239. }
  240. [self resetAddImgFrame];
  241. [self.collectionView reloadData];
  242. }
  243. }
  244. - (NSMutableArray <SHAddImgModel *>*)imageModels {
  245. if (!_imageModels) {
  246. _imageModels = [NSMutableArray array];
  247. }
  248. return _imageModels;
  249. }
  250. - (void)setConfigModel:(SHConfigModel *)configModel {
  251. _configModel = configModel;
  252. _addImgView.hidden = _configModel.addBtnHidden;
  253. _addImgView.image = [UIImage imageNamed:_configModel.placeholderName];
  254. _itemHeight = self.bounds.size.width / _maximageColumn - kItemPadding;
  255. _imageHeight = _itemHeight - kItemPadding;
  256. _addImgView.frame = CGRectMake(kItemPadding * .5, kItemPadding * .5, _imageHeight, _imageHeight);
  257. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  258. layout.itemSize = CGSizeMake(_itemHeight,_itemHeight);
  259. _collectionView.collectionViewLayout = layout;
  260. _collectionView.frame = self.bounds;
  261. }
  262. #pragma mark - UIImagePickerControllerDelegate
  263. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  264. __weak typeof(self) weakSelf = self;
  265. NSString *mediaType = info[UIImagePickerControllerMediaType];
  266. if([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
  267. UIImage *resultImage = nil;
  268. if(picker.allowsEditing){
  269. resultImage = info[UIImagePickerControllerEditedImage];
  270. } else {
  271. resultImage = info[UIImagePickerControllerOriginalImage];
  272. }
  273. SHAddImgModel *model = [[SHAddImgModel alloc] init];
  274. model.image = resultImage;
  275. [weakSelf.imageModels addObject:model];
  276. [weakSelf.images addObject:resultImage];
  277. [self.collectionView reloadData];
  278. [self resetAddImgFrame];
  279. }
  280. [picker dismissViewControllerAnimated:YES completion:nil];
  281. }
  282. #pragma mark - 拍照回调
  283. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  284. [picker dismissViewControllerAnimated:YES completion:nil];
  285. }
  286. @end