PDFBackgroundSettingViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // CPDFBackgroundSettingViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdanmobile_2 on 2023/1/2.
  6. //
  7. #import "PDFBackgroundSettingViewController.h"
  8. #import "PDFBackgroundView.h"
  9. #import "PDFBackgroundPreview.h"
  10. #import "PDFBackgroundSettingView.h"
  11. #import "PDFDrawBackgroundView.h"
  12. #import "PDFDrawImageView.h"
  13. #import "Masonry.h"
  14. @interface PDFBackgroundSettingViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
  15. @property (nonatomic,strong) PDFBackgroundView *backgroundView;
  16. @property (nonatomic,strong) PDFBackgroundPreview *backgroundPreView;
  17. @property (nonatomic,strong) PDFBackgroundSettingView *backgroudSettingView;
  18. @property (nonatomic,strong) PDFDrawImageView *drawImageView;
  19. @property (nonatomic,strong) PDFDrawBackgroundView *drawBackgroundView;
  20. @property (nonatomic,strong) PDFBackgroundModel *dataModel;
  21. @property (nonatomic,assign) CGSize imageSize;
  22. @property (nonatomic,strong) UIImage *image;
  23. @property (nonatomic,strong) CPDFDocument *document;
  24. @end
  25. @implementation PDFBackgroundSettingViewController
  26. #pragma mark - UIViewController Methods
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)];
  30. UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UIBarButtonItemStylePlain target:self action:@selector(editClick)];
  31. UIBarButtonItem *deleteBtn = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
  32. self.navigationItem.rightBarButtonItems = @[doneBtn, editBtn, deleteBtn];
  33. [self initDataMoel];
  34. [self createView];
  35. [self addConstraint];
  36. [self addTargets];
  37. }
  38. #pragma mark - Initializers
  39. - (instancetype)initWithDocument:(CPDFDocument *)document {
  40. self = [super init];
  41. if (self) {
  42. _document = document;
  43. CPDFPage *pdfPage = [document pageAtIndex:0];
  44. CGSize imageSize = [document pageSizeAtIndex:0];
  45. _image = [pdfPage thumbnailOfSize:imageSize];
  46. _imageSize = imageSize;
  47. }
  48. return self;
  49. }
  50. - (void)createView {
  51. _backgroundView = [[PDFBackgroundView alloc] init];
  52. [self.view addSubview:_backgroundView];
  53. _backgroundPreView = [[PDFBackgroundPreview alloc] init];
  54. [self.view addSubview:_backgroundPreView];
  55. _backgroundPreView.documentView.image = self.image;
  56. _drawBackgroundView = [[PDFDrawBackgroundView alloc] init];
  57. _drawBackgroundView.dataModel = self.dataModel;
  58. [self.backgroundPreView addSubview:_drawBackgroundView];
  59. _drawImageView = [[PDFDrawImageView alloc] init];
  60. [_drawImageView setImage:_image];
  61. [self.backgroundPreView addSubview:_drawImageView];
  62. }
  63. - (void)initDataMoel {
  64. _dataModel = [[PDFBackgroundModel alloc] init];
  65. _dataModel.backgroundColor = [UIColor whiteColor];
  66. _dataModel.backgroudScale = 1.0f;
  67. _dataModel.backgroundOpacity = 1.0f;
  68. _dataModel.backgroundRotation = 0.0f;
  69. _dataModel.verticalSpacing = 0.0f;
  70. _dataModel.horizontalSpacing = 0.0f;
  71. _dataModel.pageString = @"0";
  72. }
  73. - (void)addConstraint {
  74. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown || (self.view.frame.size.width > self.view.frame.size.height)) {
  75. [self addSideConstraint];
  76. } else {
  77. [self addNormalConstraint];
  78. }
  79. }
  80. - (void)addTargets {
  81. [_backgroundView.selectBtn addTarget:self action:@selector(onSelectBtnClicked:) forControlEvents:UIControlEventTouchDown];
  82. for (NSInteger i = 0; i < _backgroundView.colorArray.count; ++i) {
  83. [_backgroundView.colorArray[i] addTarget:self action:@selector(onColorBtnClicked:) forControlEvents:UIControlEventTouchDown];
  84. }
  85. }
  86. #pragma mark - Actions
  87. - (void)onClickedDoneBtn {
  88. [self.delegate PDFViewPerformChangeBackground:self.dataModel];
  89. [self.navigationController popViewControllerAnimated:YES];
  90. }
  91. - (void)editClick {
  92. self.backgroundView.selectBtn.enabled = NO;
  93. self.navigationItem.rightBarButtonItems.firstObject.enabled = NO;
  94. self.navigationItem.rightBarButtonItems.lastObject.enabled = NO;
  95. if (!([_backgroudSettingView isDescendantOfView:self.view])) {
  96. _backgroudSettingView = [[PDFBackgroundSettingView alloc] init];
  97. _backgroudSettingView.verticalField.delegate = self;
  98. _backgroudSettingView.horizontalField.delegate = self;
  99. [self.view addSubview:_backgroudSettingView];
  100. [_backgroudSettingView mas_makeConstraints:^(MASConstraintMaker *make) {
  101. make.centerX.equalTo(self.backgroundPreView.mas_centerX);
  102. make.centerY.equalTo(self.backgroundPreView.mas_centerY);
  103. make.height.mas_equalTo(330);
  104. make.width.mas_equalTo(320);
  105. }];
  106. [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  107. self.backgroudSettingView.center = CGPointMake(self.backgroudSettingView.center.x, self.backgroudSettingView.center.y - 300);
  108. } completion:nil];
  109. [_backgroudSettingView.pageBtn addTarget:self action:@selector(onSelectPageRange:) forControlEvents:UIControlEventTouchUpInside];
  110. [_backgroudSettingView.doneBtn addTarget:self action:@selector(doneClick:) forControlEvents:UIControlEventTouchUpInside];
  111. [_backgroudSettingView.cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
  112. [_backgroudSettingView.opacitySlider addTarget:self action:@selector(onOpacityChanged:) forControlEvents:UIControlEventValueChanged];
  113. [_backgroudSettingView.scaleSlider addTarget:self action:@selector(onScaleChanged:) forControlEvents:UIControlEventValueChanged];
  114. [_backgroudSettingView.rotationSlider addTarget:self action:@selector(onRotationChanged:) forControlEvents:UIControlEventValueChanged];
  115. [_backgroudSettingView.horizontalField addTarget:self action:@selector(horizontalChange:) forControlEvents:UIControlEventEditingDidEnd];
  116. [_backgroudSettingView.verticalField addTarget:self action:@selector(verticalChage:) forControlEvents:UIControlEventEditingDidEnd];
  117. }
  118. }
  119. - (void)deleteClick {
  120. [self.delegate PDFViewPerformDeleteBackground];
  121. [self.navigationController popViewControllerAnimated:YES];
  122. }
  123. #pragma mark - UI Functions
  124. - (void)onColorBtnClicked:(UIButton *)btn {
  125. if (_dataModel.image) {
  126. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Select Color Warning" message:@"Sorry,we can't choose color" preferredStyle:UIAlertControllerStyleAlert];
  127. [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  128. }]];
  129. [self presentViewController:alertController animated:YES completion:nil];
  130. }
  131. _dataModel.backgroundColor = btn.backgroundColor;
  132. _drawBackgroundView.dataModel = self.dataModel;
  133. [_drawBackgroundView setNeedsDisplay];
  134. // _backgroundPreView.documentView.image = [_image imageWithTintColor:btn.backgroundColor];
  135. }
  136. - (void)onSelectBtnClicked:(UIButton *)sender {
  137. UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  138. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  139. imagePicker.delegate = self;
  140. imagePicker.allowsEditing = YES;
  141. [self presentViewController:imagePicker animated:YES completion:nil];
  142. }
  143. - (void)onSelectPageRange:(UIButton *)btn {
  144. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  145. UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:@"All Pages" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  146. self.backgroudSettingView.rangeLabel.text = @"Page Range: ALL";
  147. self.dataModel.pageString = [NSString stringWithFormat:@"0-%lu",self.document.pageCount - 1];
  148. }];
  149. UIAlertAction *customRange = [UIAlertAction actionWithTitle:@"Custom" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  150. [self createCustomRangeAlert];
  151. }];
  152. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  153. }];
  154. [alertController addAction:defaultRange];
  155. [alertController addAction:customRange];
  156. [alertController addAction:cancelAction];
  157. [self presentViewController:alertController animated:YES completion:nil];
  158. }
  159. - (void)createCustomRangeAlert {
  160. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Custom Page Range" message:nil preferredStyle:UIAlertControllerStyleAlert];
  161. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  162. textField.placeholder = @"such as:1,3-5,10";
  163. }];
  164. [alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  165. self.dataModel.pageString = alertController.textFields.firstObject.text;
  166. self.backgroudSettingView.rangeLabel.text = @"Page Range:Custom";
  167. }]];
  168. [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  169. }]];
  170. [self presentViewController:alertController animated:YES completion:nil];
  171. }
  172. - (void)doneClick:(UIButton *)btn {
  173. [self btnEnable];
  174. _drawBackgroundView.dataModel = self.dataModel;
  175. [_drawBackgroundView setNeedsDisplay];
  176. [_backgroudSettingView removeFromSuperview];
  177. }
  178. - (void)cancelClick:(UIButton *)btn {
  179. [self btnEnable];
  180. [_backgroudSettingView removeFromSuperview];
  181. }
  182. - (void)onOpacityChanged:(UISlider *)sender {
  183. _dataModel.backgroundOpacity = 1 - sender.value;
  184. }
  185. - (void)onScaleChanged:(UISlider *)sender {
  186. _dataModel.backgroudScale = sender.value;
  187. }
  188. - (void)onRotationChanged:(UISlider *)sender {
  189. _dataModel.backgroundRotation = sender.value * 180 / M_PI;
  190. }
  191. - (void)horizontalChange:(UITextField *)text {
  192. _dataModel.horizontalSpacing = [text.text floatValue];
  193. }
  194. - (void)verticalChage:(UITextField *)text {
  195. _dataModel.verticalSpacing = [text.text floatValue];
  196. }
  197. - (void)btnEnable {
  198. self.backgroundView.selectBtn.enabled = YES;
  199. self.navigationItem.rightBarButtonItems.firstObject.enabled = YES;
  200. self.navigationItem.rightBarButtonItems.lastObject.enabled = YES;
  201. }
  202. #pragma mark - Orientation
  203. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  204. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  205. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
  206. [self addSideConstraint];
  207. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
  208. [self addNormalConstraint];
  209. }
  210. [_drawBackgroundView setNeedsDisplay];
  211. }
  212. - (void)addSideConstraint {
  213. [_backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
  214. make.bottom.equalTo(self.view.mas_bottom).offset(-25);
  215. make.left.equalTo(self.view.mas_left);
  216. make.right.equalTo(self.view.mas_right);
  217. make.height.mas_equalTo(70);
  218. }];
  219. [_backgroundPreView mas_remakeConstraints:^(MASConstraintMaker *make) {
  220. make.top.equalTo(self.view.mas_top).offset(43.6);
  221. make.left.equalTo(self.view.mas_left);
  222. make.right.equalTo(self.view.mas_right);
  223. make.bottom.equalTo(self.backgroundView.mas_top);
  224. }];
  225. [_backgroundPreView.documentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  226. make.top.equalTo(self.view.mas_top).offset(43.6);
  227. make.bottom.equalTo(self.backgroundPreView.mas_bottom);
  228. make.centerX.equalTo(self.backgroundPreView.mas_centerX);
  229. make.width.mas_equalTo(233);
  230. }];
  231. [_drawBackgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
  232. make.top.equalTo(self.backgroundPreView.documentView.mas_top);
  233. make.bottom.equalTo(self.backgroundPreView.documentView.mas_bottom);
  234. make.left.equalTo(self.backgroundPreView.documentView.mas_left);
  235. make.right.equalTo(self.backgroundPreView.documentView.mas_right);
  236. }];
  237. [_drawImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  238. make.top.equalTo(self.backgroundPreView.documentView.mas_top);
  239. make.bottom.equalTo(self.backgroundPreView.documentView.mas_bottom);
  240. make.left.equalTo(self.backgroundPreView.documentView.mas_left);
  241. make.right.equalTo(self.backgroundPreView.documentView.mas_right);
  242. }];
  243. }
  244. - (void)addNormalConstraint {
  245. [_backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
  246. make.bottom.equalTo(self.view.mas_bottom).offset(-25);
  247. make.left.equalTo(self.view.mas_left);
  248. make.right.equalTo(self.view.mas_right);
  249. make.height.mas_equalTo(70);
  250. }];
  251. [_backgroundPreView mas_remakeConstraints:^(MASConstraintMaker *make) {
  252. make.top.equalTo(self.view.mas_top).offset(83.6);
  253. make.left.equalTo(self.view.mas_left);
  254. make.right.equalTo(self.view.mas_right);
  255. make.bottom.equalTo(self.backgroundView.mas_top);
  256. }];
  257. [_backgroundPreView.documentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  258. make.left.equalTo(self.backgroundPreView.mas_left);
  259. make.top.equalTo(self.backgroundPreView.mas_top).offset(40);
  260. make.bottom.equalTo(self.backgroundPreView.mas_bottom).offset(-40);
  261. make.width.mas_equalTo(393);
  262. }];
  263. [_drawBackgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
  264. make.top.equalTo(self.backgroundPreView.documentView.mas_top);
  265. make.bottom.equalTo(self.backgroundPreView.documentView.mas_bottom);
  266. make.left.equalTo(self.backgroundPreView.documentView.mas_left);
  267. make.right.equalTo(self.backgroundPreView.documentView.mas_right);
  268. }];
  269. [_drawImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  270. make.top.equalTo(self.backgroundPreView.documentView.mas_top);
  271. make.bottom.equalTo(self.backgroundPreView.documentView.mas_bottom);
  272. make.left.equalTo(self.backgroundPreView.documentView.mas_left);
  273. make.right.equalTo(self.backgroundPreView.documentView.mas_right);
  274. }];
  275. }
  276. #pragma mark - UIImagePickerControllerDelegate
  277. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
  278. UIImage *image = info[UIImagePickerControllerOriginalImage];
  279. CGSize size;
  280. size.width = self.backgroundPreView.documentView.frame.size.width;
  281. size.height = self.backgroundPreView.documentView.frame.size.height;
  282. _dataModel.image = [self imageWithImageSimple:image scaledToSize:_imageSize];
  283. [_backgroundPreView.preImageView sizeToFit];
  284. [picker dismissViewControllerAnimated:YES completion:nil];
  285. [_drawBackgroundView setNeedsDisplay];
  286. }
  287. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  288. [picker dismissViewControllerAnimated:YES completion:nil];
  289. }
  290. - (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
  291. {
  292. UIGraphicsBeginImageContext(newSize);
  293. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  294. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  295. UIGraphicsEndImageContext();
  296. return newImage;
  297. }
  298. #pragma mark - UITextFieldDelegate
  299. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  300. [textField resignFirstResponder];
  301. return YES;
  302. }
  303. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  304. if (_backgroudSettingView.horizontalField == textField || _backgroudSettingView.verticalField == textField) {
  305. return [self validateValue:string];
  306. }
  307. return YES;
  308. }
  309. - (BOOL)validateValue:(NSString *)number {
  310. BOOL res = YES;
  311. NSCharacterSet *numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
  312. NSInteger i = 0;
  313. while (i < number.length) {
  314. NSString *str = [number substringWithRange:NSMakeRange(i, 1)];
  315. NSRange range = [str rangeOfCharacterFromSet:numberSet];
  316. if (range.length == 0) {
  317. res = NO;
  318. break;
  319. }
  320. i++;
  321. }
  322. return res;
  323. }
  324. @end