CPDFBackgroundSettingViewController.m 18 KB

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