CPDFBackgroundSettingViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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(-5);
  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(-20);
  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. if (!([_backgroudSettingView isDescendantOfView:self.view])) {
  143. _backgroudSettingView = [[CPDFBackgroundSettingView alloc] init];
  144. _backgroudSettingView.verticalField.delegate = self;
  145. _backgroudSettingView.horizontalField.delegate = self;
  146. [self.view addSubview:_backgroudSettingView];
  147. [_backgroudSettingView mas_makeConstraints:^(MASConstraintMaker *make) {
  148. make.centerX.equalTo(self.backgroundPreView.mas_centerX);
  149. make.centerY.equalTo(self.backgroundPreView.mas_centerY);
  150. make.height.mas_equalTo(330);
  151. make.width.mas_equalTo(320);
  152. }];
  153. [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  154. self.backgroudSettingView.center = CGPointMake(self.backgroudSettingView.center.x, self.backgroudSettingView.center.y - 300);
  155. } completion:nil];
  156. [_backgroudSettingView.pageBtn addTarget:self action:@selector(onSelectPageRange:) forControlEvents:UIControlEventTouchUpInside];
  157. [_backgroudSettingView.doneBtn addTarget:self action:@selector(doneClick:) forControlEvents:UIControlEventTouchUpInside];
  158. [_backgroudSettingView.cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
  159. [_backgroudSettingView.opacitySlider addTarget:self action:@selector(onOpacityChanged:) forControlEvents:UIControlEventValueChanged];
  160. [_backgroudSettingView.scaleSlider addTarget:self action:@selector(onScaleChanged:) forControlEvents:UIControlEventValueChanged];
  161. [_backgroudSettingView.rotationSlider addTarget:self action:@selector(onRotationChanged:) forControlEvents:UIControlEventValueChanged];
  162. [_backgroudSettingView.horizontalField addTarget:self action:@selector(horizontalChange:) forControlEvents:UIControlEventEditingDidEnd];
  163. [_backgroudSettingView.verticalField addTarget:self action:@selector(verticalChage:) forControlEvents:UIControlEventEditingDidEnd];
  164. }
  165. }
  166. - (void)deleteClick {
  167. [self.delegate deleteBackgroundModel];
  168. [self.navigationController popViewControllerAnimated:YES];
  169. }
  170. #pragma mark - UI Functions
  171. - (void)onColorBtnClicked:(UIButton *)btn {
  172. _dataModel.backgroundColor = btn.backgroundColor;
  173. _drawBackgroundView.dataModel = self.dataModel;
  174. [_drawBackgroundView setNeedsDisplay];
  175. // _backgroundPreView.documentView.image = [_image imageWithTintColor:btn.backgroundColor];
  176. }
  177. - (void)onSelectBtnClicked:(UIButton *)sender {
  178. UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  179. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  180. imagePicker.delegate = self;
  181. imagePicker.allowsEditing = YES;
  182. [self presentViewController:imagePicker animated:YES completion:nil];
  183. }
  184. - (void)onSelectPageRange:(UIButton *)btn {
  185. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  186. UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:@"All Pages" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  187. self.backgroudSettingView.rangeLabel.text = @"Page Range: ALL";
  188. }];
  189. UIAlertAction *customRange = [UIAlertAction actionWithTitle:@"Custom" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  190. [self createCustomRangeAlert];
  191. }];
  192. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  193. }];
  194. [alertController addAction:defaultRange];
  195. [alertController addAction:customRange];
  196. [alertController addAction:cancelAction];
  197. [self presentViewController:alertController animated:YES completion:nil];
  198. }
  199. - (void)createCustomRangeAlert {
  200. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Custom Page Range" message:nil preferredStyle:UIAlertControllerStyleAlert];
  201. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  202. textField.placeholder = @"such as:1,3-5,10";
  203. }];
  204. [alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  205. self.dataModel.pageString = alertController.textFields.firstObject.text;
  206. self.backgroudSettingView.rangeLabel.text = @"Page Range:Custom";
  207. }]];
  208. [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  209. }]];
  210. [self presentViewController:alertController animated:YES completion:nil];
  211. }
  212. - (void)doneClick:(UIButton *)btn {
  213. _drawBackgroundView.dataModel = self.dataModel;
  214. [_drawBackgroundView setNeedsDisplay];
  215. [_backgroudSettingView removeFromSuperview];
  216. }
  217. - (void)cancelClick:(UIButton *)btn {
  218. [_backgroudSettingView removeFromSuperview];
  219. }
  220. - (void)onOpacityChanged:(UISlider *)sender {
  221. _dataModel.backgroundOpacity = 1 - sender.value;
  222. }
  223. - (void)onScaleChanged:(UISlider *)sender {
  224. _dataModel.backgroudScale = sender.value;
  225. }
  226. - (void)onRotationChanged:(UISlider *)sender {
  227. _dataModel.backgroundRotation = sender.value * 180 / M_PI;
  228. }
  229. - (void)horizontalChange:(UITextField *)text {
  230. _dataModel.horizontalSpacing = [text.text floatValue];
  231. }
  232. - (void)verticalChage:(UITextField *)text {
  233. _dataModel.verticalSpacing = [text.text floatValue];
  234. }
  235. #pragma mark - Orientation
  236. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  237. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  238. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
  239. [_backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
  240. make.bottom.equalTo(self.view.mas_bottom).offset(-5);
  241. make.left.equalTo(self.view.mas_left);
  242. make.right.equalTo(self.view.mas_right);
  243. make.height.mas_equalTo(70);
  244. }];
  245. [_backgroundPreView mas_remakeConstraints:^(MASConstraintMaker *make) {
  246. make.top.equalTo(self.view.mas_top).offset(43.6);
  247. make.left.equalTo(self.view.mas_left);
  248. make.right.equalTo(self.view.mas_right);
  249. make.bottom.equalTo(self.backgroundView.mas_top);
  250. }];
  251. [_backgroundPreView.documentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  252. make.top.equalTo(self.backgroundPreView.mas_top);
  253. make.bottom.equalTo(self.backgroundPreView.mas_bottom);
  254. make.centerX.equalTo(self.backgroundPreView.mas_centerX);
  255. make.width.mas_equalTo(233);
  256. }];
  257. [_drawBackgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
  258. make.top.equalTo(self.backgroundPreView.documentView.mas_top);
  259. make.bottom.equalTo(self.backgroundPreView.documentView.mas_bottom);
  260. make.left.equalTo(self.backgroundPreView.documentView.mas_left);
  261. make.right.equalTo(self.backgroundPreView.documentView.mas_right);
  262. }];
  263. [_drawImageView 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. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
  270. [_backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
  271. make.bottom.equalTo(self.view.mas_bottom).offset(-20);
  272. make.left.equalTo(self.view.mas_left);
  273. make.right.equalTo(self.view.mas_right);
  274. make.height.mas_equalTo(70);
  275. }];
  276. [_backgroundPreView mas_remakeConstraints:^(MASConstraintMaker *make) {
  277. make.top.equalTo(self.view.mas_top).offset(83.6);
  278. make.left.equalTo(self.view.mas_left);
  279. make.right.equalTo(self.view.mas_right);
  280. make.bottom.equalTo(self.backgroundView.mas_top);
  281. }];
  282. [_backgroundPreView.documentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  283. make.left.equalTo(self.view.mas_left);
  284. make.top.equalTo(self.view.mas_top).offset(123.6);
  285. make.bottom.equalTo(self.backgroundPreView.mas_bottom).offset(-40);
  286. make.width.mas_equalTo(393);
  287. }];
  288. }
  289. [_drawBackgroundView setNeedsDisplay];
  290. }
  291. #pragma mark - UIImagePickerControllerDelegate
  292. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
  293. UIImage *image = info[UIImagePickerControllerOriginalImage];
  294. CGSize size;
  295. size.width = self.backgroundPreView.documentView.frame.size.width;
  296. size.height = self.backgroundPreView.documentView.frame.size.height;
  297. // _backgroundPreView.imageView.image = [self imageWithImageSimple:image scaledToSize:size];
  298. _dataModel.image = [self imageWithImageSimple:image scaledToSize:_imageSize];
  299. [_backgroundPreView.imageView sizeToFit];
  300. [picker dismissViewControllerAnimated:YES completion:nil];
  301. [_drawBackgroundView setNeedsDisplay];
  302. }
  303. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  304. [picker dismissViewControllerAnimated:YES completion:nil];
  305. }
  306. - (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
  307. {
  308. UIGraphicsBeginImageContext(newSize);
  309. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  310. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  311. UIGraphicsEndImageContext();
  312. return newImage;
  313. }
  314. #pragma mark - UITextFieldDelegate
  315. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  316. [textField resignFirstResponder];
  317. return YES;
  318. }
  319. @end