CPDFImageViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // CPDFImageViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdan on 2022/11/19.
  6. //
  7. #import "CPDFImageViewController.h"
  8. #import "CPDFImageView.h"
  9. #import "CPDFImagePreview.h"
  10. #import "CPDFDataModel.h"
  11. #import "Masonry.h"
  12. @interface CPDFImageViewController ()
  13. @property (nonatomic,assign) CGRect watermarkFrame;
  14. @end
  15. @implementation CPDFImageViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. // Do any additional setup after loading the view.
  19. _imagePreview = [[CPDFImagePreview alloc] init];
  20. _imageView = [[CPDFImageView alloc] init];
  21. _dataModel = [[CPDFDataModel alloc] init];
  22. [self.view addSubview:_imagePreview];
  23. [self.view addSubview:_imageView];
  24. [self.view setBackgroundColor:UIColor.systemGray5Color];
  25. [self initDataModel];
  26. [self addConstraint];
  27. [self addTargets];
  28. [self createGestureRecognizer];
  29. }
  30. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  31. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  32. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
  33. [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  34. make.right.equalTo(self.view.mas_right).offset(-30);
  35. make.left.equalTo(_imagePreview.mas_right).offset(0);
  36. make.height.equalTo(@205);
  37. make.width.equalTo(@(self.view.bounds.size.width));
  38. make.bottom.equalTo(self.view.mas_bottom).offset(-55);
  39. }];
  40. [_imagePreview mas_remakeConstraints:^(MASConstraintMaker *make) {
  41. make.top.equalTo(self.view.mas_top).offset(10);
  42. make.right.equalTo(_imageView.mas_left).offset(-5);
  43. make.left.equalTo(self.view.mas_left).offset(0);
  44. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  45. }];
  46. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
  47. [_imagePreview mas_remakeConstraints:^(MASConstraintMaker *make) {
  48. make.top.equalTo(self.view.mas_top).offset(0);
  49. make.width.equalTo(self.view.mas_width);
  50. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  51. }];
  52. [_imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  53. make.top.equalTo(_imagePreview.mas_bottom).offset(0);
  54. make.width.equalTo(_imagePreview.mas_width);
  55. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  56. }];
  57. }
  58. }
  59. - (void)initDataModel {
  60. _dataModel.watermarkOpacity = 1;
  61. _dataModel.imagePath = @"btn_selected";
  62. _dataModel.watermarkScale = 1;
  63. _dataModel.isTile = NO;
  64. _dataModel.watermarkRotation = 0;
  65. _imageView.horizontalField.text = 0;
  66. _imageView.verticalField.text = 0;
  67. _imageView.horizontalField.enabled = NO;
  68. _imageView.verticalField.enabled = NO;
  69. _imageView.imageScaleSlider.enabled = YES;
  70. _imagePreview.watermarkView.image = [UIImage imageNamed:_dataModel.imagePath];
  71. }
  72. - (void)addConstraint {
  73. [_imagePreview mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.top.equalTo(self.view.mas_top).offset(0);
  75. make.width.equalTo(self.view.mas_width);
  76. make.bottom.equalTo(self.view.mas_bottom).offset(-205);
  77. }];
  78. [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  79. make.top.equalTo(_imagePreview.mas_bottom).offset(0);
  80. make.width.equalTo(_imagePreview.mas_width);
  81. make.bottom.equalTo(self.view.mas_bottom).offset(0);
  82. }];
  83. }
  84. - (void)addTargets {
  85. [_imageView.selectBtn addTarget:self action:@selector(onSelectBtnClicked:) forControlEvents:UIControlEventTouchDown];
  86. [_imageView.opacitySlider addTarget:self action:@selector(onOpacityChanged:) forControlEvents:UIControlEventValueChanged];
  87. [_imageView.imageScaleSlider addTarget:self action:@selector(onImageScaleChanged:) forControlEvents:UIControlEventValueChanged];
  88. [_imageView.tileSwitch addTarget:self action:@selector(onTileSwitchChanged:) forControlEvents:UIControlEventValueChanged];
  89. [_imageView.pageBtn addTarget:self action:@selector(onSelectPageRange:) forControlEvents:UIControlEventTouchDown];
  90. }
  91. - (void)createGestureRecognizer {
  92. [_imagePreview.documentView setUserInteractionEnabled:YES];
  93. [_imagePreview.watermarkView setMultipleTouchEnabled:YES];
  94. [_imagePreview.watermarkView setUserInteractionEnabled:YES];
  95. [_imagePreview.rotationBtn setUserInteractionEnabled:YES];
  96. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWatermarkView:)];
  97. [_imagePreview.watermarkView addGestureRecognizer:panRecognizer];
  98. UIPanGestureRecognizer *panRotationBtnRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateWatermarkView:)];
  99. [_imagePreview.rotationBtn addGestureRecognizer:panRotationBtnRecognizer];
  100. }
  101. - (void)rotateWatermarkView:(UIPanGestureRecognizer *)recognizer {
  102. CGPoint point = [recognizer translationInView:_imagePreview];
  103. CGFloat radian = atan2(point.x + _imagePreview.center.x - _imagePreview.watermarkView.center.x,point.y + _imagePreview.center.y - _imagePreview.watermarkView.center.y);
  104. _imagePreview.watermarkView.transform = CGAffineTransformMakeRotation(-radian);
  105. _dataModel.watermarkRotation = (-radian) * 180 / M_PI;
  106. }
  107. - (void)panWatermarkView:(UIPanGestureRecognizer *)recognizer {
  108. CGPoint point = [recognizer translationInView:_imagePreview.documentView];
  109. CGRect documentFrame = _imagePreview.documentView.frame;
  110. documentFrame.origin.x -= _imagePreview.documentView.frame.origin.x;
  111. documentFrame.origin.y -= _imagePreview.documentView.frame.origin.y;
  112. [_imagePreview.watermarkView setCenter:CGPointMake(_imagePreview.watermarkView.center.x + point.x, _imagePreview.watermarkView.center.y + point.y)];
  113. [_imagePreview.rotationBtn setCenter:CGPointMake(_imagePreview.rotationBtn.center.x + point.x, _imagePreview.rotationBtn.center.y + point.y)];
  114. if (!CGRectContainsRect(documentFrame,_imagePreview.watermarkView.frame)) {
  115. [_imagePreview.watermarkView setCenter:CGPointMake(_imagePreview.watermarkView.center.x - point.x, _imagePreview.watermarkView.center.y - point.y)];
  116. [_imagePreview.rotationBtn setCenter:CGPointMake(_imagePreview.rotationBtn.center.x - point.x, _imagePreview.rotationBtn.center.y - point.y)];
  117. }
  118. _dataModel.tx = _imagePreview.watermarkView.frame.origin.x - (_imagePreview.documentView.center.x - _imagePreview.documentView.frame.origin.x);
  119. _dataModel.ty = _imagePreview.documentView.center.y - _imagePreview.documentView.frame.origin.y - _imagePreview.watermarkView.frame.origin.y;
  120. [recognizer setTranslation:CGPointZero inView:_imagePreview.documentView];
  121. }
  122. - (void)onSelectBtnClicked:(UIButton *)sender {
  123. }
  124. - (void)onOpacityChanged:(UISlider *)sender {
  125. _imagePreview.watermarkView.alpha = 1 - sender.value;
  126. [_dataModel setWatermarkOpacity:1 - sender.value];
  127. }
  128. - (void)onImageScaleChanged:(UISlider *)sender {
  129. sender.minimumValue = 1;
  130. sender.maximumValue = 3;
  131. _watermarkFrame.size.width = sender.value * [UIImage imageNamed:_dataModel.imagePath].size.width;
  132. _watermarkFrame.size.height = sender.value * [UIImage imageNamed:_dataModel.imagePath].size.height;;
  133. _watermarkFrame.origin.x = _imagePreview.watermarkView.frame.origin.x;
  134. _watermarkFrame.origin.y = _imagePreview.watermarkView.frame.origin.y;
  135. _imagePreview.watermarkView.frame = _watermarkFrame;
  136. _imagePreview.watermarkFrame = _watermarkFrame;
  137. [_imagePreview.rotationBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  138. make.top.equalTo(_imagePreview.watermarkView.mas_bottom).offset(-10);
  139. make.width.equalTo(@20);
  140. make.height.equalTo(@20);
  141. make.left.equalTo(_imagePreview.watermarkView.mas_right).offset(-10);
  142. }];
  143. [_dataModel setWatermarkScale:sender.value];
  144. }
  145. - (void)onSelectPageRange:(UIButton *)sender {
  146. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  147. UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:@"All Pages" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  148. self.imageView.rangeLabel.text = @"Page Range: ALL";
  149. }];
  150. UIAlertAction *customRange = [UIAlertAction actionWithTitle:@"Custom Page Range" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  151. [self createCustomRangeAlert];
  152. }];
  153. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  154. }];
  155. [alertController addAction:defaultRange];
  156. [alertController addAction:customRange];
  157. [alertController addAction:cancelAction];
  158. [self presentViewController:alertController animated:YES completion:nil];
  159. }
  160. - (void)createCustomRangeAlert {
  161. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Custom Page Range" message:nil preferredStyle:UIAlertControllerStyleAlert];
  162. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  163. textField.placeholder = @"such as:1,3-5,10";
  164. }];
  165. [alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  166. self.dataModel.pageString = alertController.textFields.firstObject.text;
  167. self.imageView.rangeLabel.text = @"Page Range:Custom";
  168. }]];
  169. [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  170. }]];
  171. [self presentViewController:alertController animated:YES completion:nil];
  172. }
  173. - (void)onTileSwitchChanged:(UISwitch *) sender {
  174. if ([sender isOn]) {
  175. _dataModel.isTile = YES;
  176. _imageView.horizontalField.enabled = YES;
  177. _imageView.verticalField.enabled = YES;
  178. _imageView.imageScaleSlider.enabled = NO;
  179. } else {
  180. _dataModel.isTile = NO;
  181. _imageView.horizontalField.enabled = NO;
  182. _imageView.verticalField.enabled = NO;
  183. _imageView.imageScaleSlider.enabled = YES;
  184. }
  185. }
  186. @end