123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- //
- // CPDFBackgroundSettingViewController.m
- // PDFViewer
- //
- // Created by kdanmobile_2 on 2023/1/2.
- //
- #import "PDFBackgroundSettingViewController.h"
- #import "PDFBackgroundView.h"
- #import "PDFBackgroundPreview.h"
- #import "PDFBackgroundSettingView.h"
- #import "PDFDrawBackgroundView.h"
- #import "PDFDrawImageView.h"
- #import "Masonry.h"
- @interface PDFBackgroundSettingViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
- @property (nonatomic,strong) PDFBackgroundView *backgroundView;
- @property (nonatomic,strong) PDFBackgroundPreview *backgroundPreView;
- @property (nonatomic,strong) PDFBackgroundSettingView *backgroudSettingView;
- @property (nonatomic,strong) PDFDrawImageView *drawImageView;
- @property (nonatomic,strong) PDFDrawBackgroundView *drawBackgroundView;
- @property (nonatomic,strong) PDFBackgroundModel *dataModel;
- @property (nonatomic,assign) CGSize imageSize;
- @property (nonatomic,strong) UIImage *image;
- @property (nonatomic,strong) CPDFDocument *document;
- @end
- @implementation PDFBackgroundSettingViewController
- #pragma mark - UIViewController Methods
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)];
-
- UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UIBarButtonItemStylePlain target:self action:@selector(editClick)];
-
- UIBarButtonItem *deleteBtn = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
-
- self.navigationItem.rightBarButtonItems = @[doneBtn, editBtn, deleteBtn];
-
- [self initDataMoel];
- [self createView];
- [self addConstraint];
- [self addTargets];
- }
- #pragma mark - Initializers
- - (instancetype)initWithDocument:(CPDFDocument *)document {
- self = [super init];
- if (self) {
- _document = document;
-
- CPDFPage *pdfPage = [document pageAtIndex:0];
- CGSize imageSize = [document pageSizeAtIndex:0];
- _image = [pdfPage thumbnailOfSize:imageSize];
- _imageSize = imageSize;
- }
- return self;
- }
- - (void)createView {
- _backgroundView = [[PDFBackgroundView alloc] init];
- [self.view addSubview:_backgroundView];
-
- _backgroundPreView = [[PDFBackgroundPreview alloc] init];
- [self.view addSubview:_backgroundPreView];
-
- _backgroundPreView.documentImageView.image = self.image;
-
- _drawBackgroundView = [[PDFDrawBackgroundView alloc] init];
- _drawBackgroundView.dataModel = self.dataModel;
- [self.backgroundPreView addSubview:_drawBackgroundView];
-
- _drawImageView = [[PDFDrawImageView alloc] init];
- [_drawImageView setImage:_image];
- [self.backgroundPreView addSubview:_drawImageView];
- }
- - (void)initDataMoel {
- _dataModel = [[PDFBackgroundModel alloc] init];
- _dataModel.backgroundColor = [UIColor whiteColor];
- _dataModel.backgroudScale = 1.0f;
- _dataModel.backgroundOpacity = 1.0f;
- _dataModel.backgroundRotation = 0.0f;
- _dataModel.verticalSpacing = 0.0f;
- _dataModel.horizontalSpacing = 0.0f;
- _dataModel.pageString = @"0";
- }
- - (void)addConstraint {
- if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown || (self.view.frame.size.width > self.view.frame.size.height)) {
- [self addSideConstraint];
- } else {
- [self addNormalConstraint];
- }
- }
- - (void)addTargets {
- [_backgroundView.selectBtn addTarget:self action:@selector(onSelectBtnClicked:) forControlEvents:UIControlEventTouchDown];
-
- for (NSInteger i = 0; i < _backgroundView.colorArray.count; ++i) {
- [_backgroundView.colorArray[i] addTarget:self action:@selector(onColorBtnClicked:) forControlEvents:UIControlEventTouchDown];
- }
- }
- #pragma mark - Actions
- - (void)onClickedDoneBtn {
- [self.delegate PDFViewPerformChangeBackground:self.dataModel];
-
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)editClick {
- self.backgroundView.selectBtn.enabled = NO;
- self.navigationItem.rightBarButtonItems.firstObject.enabled = NO;
- self.navigationItem.rightBarButtonItems.lastObject.enabled = NO;
-
- if (!([_backgroudSettingView isDescendantOfView:self.view])) {
- _backgroudSettingView = [[PDFBackgroundSettingView alloc] init];
-
- _backgroudSettingView.verticalField.delegate = self;
- _backgroudSettingView.horizontalField.delegate = self;
-
- [self.view addSubview:_backgroudSettingView];
-
- [_backgroudSettingView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.backgroundPreView.mas_centerX);
- make.centerY.equalTo(self.backgroundPreView.mas_centerY);
- make.height.mas_equalTo(330);
- make.width.mas_equalTo(320);
- }];
-
- [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
- self.backgroudSettingView.center = CGPointMake(self.backgroudSettingView.center.x, self.backgroudSettingView.center.y - 300);
- } completion:nil];
-
- [_backgroudSettingView.pageBtn addTarget:self action:@selector(onSelectPageRange:) forControlEvents:UIControlEventTouchUpInside];
- [_backgroudSettingView.doneBtn addTarget:self action:@selector(doneClick:) forControlEvents:UIControlEventTouchUpInside];
- [_backgroudSettingView.cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
-
- [_backgroudSettingView.opacitySlider addTarget:self action:@selector(onOpacityChanged:) forControlEvents:UIControlEventValueChanged];
- [_backgroudSettingView.scaleSlider addTarget:self action:@selector(onScaleChanged:) forControlEvents:UIControlEventValueChanged];
- [_backgroudSettingView.rotationSlider addTarget:self action:@selector(onRotationChanged:) forControlEvents:UIControlEventValueChanged];
-
- [_backgroudSettingView.horizontalField addTarget:self action:@selector(horizontalChange:) forControlEvents:UIControlEventEditingDidEnd];
- [_backgroudSettingView.verticalField addTarget:self action:@selector(verticalChage:) forControlEvents:UIControlEventEditingDidEnd];
- }
- }
- - (void)deleteClick {
- [self.delegate PDFViewPerformDeleteBackground];
-
- [self.navigationController popViewControllerAnimated:YES];
- }
- #pragma mark - UI Functions
- - (void)onColorBtnClicked:(UIButton *)btn {
-
- if (_dataModel.image) {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Select Color Warning" message:@"Sorry,we can't choose color" preferredStyle:UIAlertControllerStyleAlert];
-
- [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- }]];
- [self presentViewController:alertController animated:YES completion:nil];
- }
-
- _dataModel.backgroundColor = btn.backgroundColor;
- _drawBackgroundView.dataModel = self.dataModel;
- [_drawBackgroundView setNeedsDisplay];
-
- // _backgroundPreView.documentView.image = [_image imageWithTintColor:btn.backgroundColor];
- }
- - (void)onSelectBtnClicked:(UIButton *)sender {
- UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
- imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- imagePicker.delegate = self;
- imagePicker.allowsEditing = YES;
- [self presentViewController:imagePicker animated:YES completion:nil];
- }
- - (void)onSelectPageRange:(UIButton *)btn {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:@"All Pages" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- self.backgroudSettingView.rangeLabel.text = @"Page Range: ALL";
- self.dataModel.pageString = [NSString stringWithFormat:@"0-%lu",self.document.pageCount - 1];
- }];
- UIAlertAction *customRange = [UIAlertAction actionWithTitle:@"Custom" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self createCustomRangeAlert];
- }];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
-
- }];
- [alertController addAction:defaultRange];
- [alertController addAction:customRange];
- [alertController addAction:cancelAction];
- [self presentViewController:alertController animated:YES completion:nil];
- }
- - (void)createCustomRangeAlert {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Custom Page Range" message:nil preferredStyle:UIAlertControllerStyleAlert];
-
- [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
- textField.placeholder = @"such as:1,3-5,10";
- }];
- [alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- self.dataModel.pageString = alertController.textFields.firstObject.text;
- self.backgroudSettingView.rangeLabel.text = @"Page Range:Custom";
- }]];
- [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- }]];
- [self presentViewController:alertController animated:YES completion:nil];
- }
- - (void)doneClick:(UIButton *)btn {
- [self btnEnable];
- _drawBackgroundView.dataModel = self.dataModel;
-
- [_drawBackgroundView setNeedsDisplay];
- [_backgroudSettingView removeFromSuperview];
- }
- - (void)cancelClick:(UIButton *)btn {
- [self btnEnable];
- [_backgroudSettingView removeFromSuperview];
- }
- - (void)onOpacityChanged:(UISlider *)sender {
- _dataModel.backgroundOpacity = 1 - sender.value;
- }
- - (void)onScaleChanged:(UISlider *)sender {
- _dataModel.backgroudScale = sender.value;
- }
- - (void)onRotationChanged:(UISlider *)sender {
- _dataModel.backgroundRotation = sender.value * 180 / M_PI;
- }
- - (void)horizontalChange:(UITextField *)text {
- _dataModel.horizontalSpacing = [text.text floatValue];
- }
- - (void)verticalChage:(UITextField *)text {
- _dataModel.verticalSpacing = [text.text floatValue];
- }
- - (void)btnEnable {
- self.backgroundView.selectBtn.enabled = YES;
- self.navigationItem.rightBarButtonItems.firstObject.enabled = YES;
- self.navigationItem.rightBarButtonItems.lastObject.enabled = YES;
- }
- #pragma mark - Orientation
- - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
- [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
- if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
-
- [self addSideConstraint];
- } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
-
- [self addNormalConstraint];
- }
-
- [_drawBackgroundView setNeedsDisplay];
- }
- - (void)addSideConstraint {
- [_backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.view.mas_bottom).offset(-25);
- make.left.equalTo(self.view.mas_left);
- make.right.equalTo(self.view.mas_right);
- make.height.mas_equalTo(70);
- }];
-
- [_backgroundPreView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.view.mas_top).offset(43.6);
- make.left.equalTo(self.view.mas_left);
- make.right.equalTo(self.view.mas_right);
- make.bottom.equalTo(self.backgroundView.mas_top);
- }];
-
- [_backgroundPreView.documentImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.view.mas_top).offset(43.6);
- make.bottom.equalTo(self.backgroundPreView.mas_bottom);
- make.centerX.equalTo(self.backgroundPreView.mas_centerX);
- make.width.mas_equalTo(233);
- }];
-
- [_drawBackgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.backgroundPreView.documentImageView.mas_top);
- make.bottom.equalTo(self.backgroundPreView.documentImageView.mas_bottom);
- make.left.equalTo(self.backgroundPreView.documentImageView.mas_left);
- make.right.equalTo(self.backgroundPreView.documentImageView.mas_right);
- }];
- [_drawImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.backgroundPreView.documentImageView.mas_top);
- make.bottom.equalTo(self.backgroundPreView.documentImageView.mas_bottom);
- make.left.equalTo(self.backgroundPreView.documentImageView.mas_left);
- make.right.equalTo(self.backgroundPreView.documentImageView.mas_right);
- }];
- }
- - (void)addNormalConstraint {
- [_backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.view.mas_bottom).offset(-25);
- make.left.equalTo(self.view.mas_left);
- make.right.equalTo(self.view.mas_right);
- make.height.mas_equalTo(70);
- }];
-
- [_backgroundPreView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.view.mas_top).offset(83.6);
- make.left.equalTo(self.view.mas_left);
- make.right.equalTo(self.view.mas_right);
- make.bottom.equalTo(self.backgroundView.mas_top);
- }];
-
- [_backgroundPreView.documentImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.backgroundPreView.mas_left);
- make.top.equalTo(self.backgroundPreView.mas_top).offset(40);
- make.bottom.equalTo(self.backgroundPreView.mas_bottom).offset(-40);
- make.width.mas_equalTo(393);
- }];
-
- [_drawBackgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.backgroundPreView.documentImageView.mas_top);
- make.bottom.equalTo(self.backgroundPreView.documentImageView.mas_bottom);
- make.left.equalTo(self.backgroundPreView.documentImageView.mas_left);
- make.right.equalTo(self.backgroundPreView.documentImageView.mas_right);
- }];
- [_drawImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.backgroundPreView.documentImageView.mas_top);
- make.bottom.equalTo(self.backgroundPreView.documentImageView.mas_bottom);
- make.left.equalTo(self.backgroundPreView.documentImageView.mas_left);
- make.right.equalTo(self.backgroundPreView.documentImageView.mas_right);
- }];
- }
- #pragma mark - UIImagePickerControllerDelegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
- UIImage *image = info[UIImagePickerControllerOriginalImage];
- CGSize size;
- size.width = self.backgroundPreView.documentImageView.frame.size.width;
- size.height = self.backgroundPreView.documentImageView.frame.size.height;
- _dataModel.image = [self imageWithImageSimple:image scaledToSize:_imageSize];
- [_backgroundPreView.preImageView sizeToFit];
-
- [picker dismissViewControllerAnimated:YES completion:nil];
- [_drawBackgroundView setNeedsDisplay];
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- - (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
- {
- UIGraphicsBeginImageContext(newSize);
- [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
- UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- #pragma mark - UITextFieldDelegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- [textField resignFirstResponder];
-
- return YES;
- }
- - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
- if (_backgroudSettingView.horizontalField == textField || _backgroudSettingView.verticalField == textField) {
- return [self validateValue:string];
- }
-
- return YES;
- }
- - (BOOL)validateValue:(NSString *)number {
- BOOL res = YES;
- NSCharacterSet *numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
-
- NSInteger i = 0;
- while (i < number.length) {
- NSString *str = [number substringWithRange:NSMakeRange(i, 1)];
- NSRange range = [str rangeOfCharacterFromSet:numberSet];
-
- if (range.length == 0) {
- res = NO;
- break;
- }
- i++;
- }
-
- return res;
- }
- @end
|