MHPrintView.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // MHPrintView.m
  3. //水印
  4. #import "MHPrintView.h"
  5. #import "MHBeautyParams.h"
  6. #import "MHBeautiesModel.h"
  7. #import "MHBeautyMenuCell.h"
  8. //图片名称
  9. static NSString *Cancel = @"print_cancel";
  10. static NSString *TOP_LEFT = @"top_left";
  11. static NSString *TOP_RIGHT = @"top_right";
  12. static NSString *BOTTOM_LEFT = @"bottom_left";
  13. static NSString *BOTTOM_RIGHT = @"bottom_right";
  14. @interface MHPrintView()
  15. <UICollectionViewDelegate,UICollectionViewDataSource>
  16. @property (nonatomic, strong) UICollectionView *collectionView;
  17. @property (nonatomic, strong) NSMutableArray *array;
  18. @property (nonatomic, assign) NSInteger lastIndex;
  19. @property (nonatomic, strong) NSMutableArray *printArr;
  20. @end
  21. @implementation MHPrintView
  22. - (instancetype)initWithFrame:(CGRect)frame {
  23. if (self = [super initWithFrame:frame]) {
  24. [self addSubview:self.collectionView];
  25. }
  26. return self;
  27. }
  28. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  29. return self.printArr.count;
  30. }
  31. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  32. MHBeautyMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHMagnifiedEffectCell" forIndexPath:indexPath];
  33. cell.watermarkModel = self.printArr[indexPath.row];
  34. return cell;
  35. }
  36. - (CGSize)collectionView:(UICollectionView *)collectionView
  37. layout:(UICollectionViewLayout *)collectionViewLayout
  38. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  39. ///修改MHUI
  40. return CGSizeMake((window_width-60-10*3)/4, MHMeiyanMenusCellHeight);
  41. }
  42. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  43. if (self.lastIndex == indexPath.row) {
  44. return;
  45. }
  46. MHBeautiesModel *model = self.printArr[indexPath.row];
  47. model.isSelected = !model.isSelected;
  48. MHBeautiesModel *lastModel = self.printArr[self.lastIndex];
  49. lastModel.isSelected = !lastModel.isSelected;
  50. NSIndexPath *lastpath = [NSIndexPath indexPathForRow:self.lastIndex inSection:0];
  51. [collectionView reloadItemsAtIndexPaths:@[indexPath,lastpath]];
  52. self.lastIndex = indexPath.row;
  53. if ([self.delegate respondsToSelector:@selector(handlePrint:)]) {
  54. [self.delegate handlePrint:model];
  55. }
  56. if (isSaveWatermark) {
  57. NSDictionary *info = @{@"KWatermarkName":model.imgName,
  58. @"kWatermarkAliment":@(model.aliment)
  59. };
  60. [[NSUserDefaults standardUserDefaults] setObject:info forKey:kMHWatermark];
  61. }
  62. }
  63. -(NSMutableArray *)printArr {
  64. if (!_printArr) {
  65. NSArray *arr = @[Cancel,TOP_LEFT,TOP_RIGHT,BOTTOM_LEFT,BOTTOM_RIGHT];
  66. _printArr = [NSMutableArray array];
  67. for (int i = 0; i<arr.count; i++) {
  68. MHBeautiesModel *model = [[MHBeautiesModel alloc] init];
  69. model.imgName = arr[i];
  70. model.aliment = i;
  71. model.menuType = MHBeautyMenuType_Watermark;
  72. if (isSaveWatermark) {
  73. NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:kMHWatermark];
  74. if (dic) {
  75. NSString *name = dic[@"KWatermarkName"];
  76. if ([model.imgName isEqualToString:name]) {
  77. model.isSelected = YES;
  78. self.lastIndex = i;
  79. }else{
  80. model.isSelected = NO;
  81. }
  82. }else{
  83. model.isSelected = i == 0 ? YES : NO;
  84. }
  85. }else{
  86. model.isSelected = i == 0 ? YES : NO;
  87. }
  88. [_printArr addObject:model];
  89. }
  90. }
  91. return _printArr;
  92. }
  93. - (UICollectionView *)collectionView {
  94. if (!_collectionView) {
  95. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  96. layout.minimumLineSpacing = 0;
  97. layout.minimumInteritemSpacing = 10;
  98. layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
  99. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  100. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, window_width, self.frame.size.height) collectionViewLayout:layout];
  101. _collectionView.backgroundColor = [UIColor clearColor];
  102. _collectionView.delegate = self;
  103. _collectionView.dataSource = self;
  104. [_collectionView registerClass:[MHBeautyMenuCell class] forCellWithReuseIdentifier:@"MHMagnifiedEffectCell"];
  105. }
  106. return _collectionView;
  107. }
  108. @end