CPDFBackgroundSettingViewController.m 18 KB

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