// // PDFFreehandViewController.h // PDFReader // // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved. // // The PDF Reader Sample applications are licensed with a modified BSD license. // Please see License for details. This notice may not be removed from this file. // #import "PDFFreehandViewController.h" #import "ColorView.h" @interface PDFFreehandDrawView : UIView @property (nonatomic,retain) UIColor *lineColor; @property (nonatomic,assign) CGFloat lineWidth; @property (nonatomic,assign) CGFloat lineOpacity; @end @implementation PDFFreehandDrawView - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _lineColor = [[UIColor redColor] retain]; _lineOpacity = 0.7; _lineWidth = 3.0; } return self; } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; CGContextRef ctx = UIGraphicsGetCurrentContext(); if (!ctx) { return; } float tOffsetX = 20.0; float tSpaceX = (self.bounds.size.width - 2*tOffsetX) / 3.0; float tSpaceY = 50.0; CGContextSetRGBFillColor(ctx,240.0/255.0, 240.0/255.0, 240.0/255.0, 1.0); CGContextFillRect(ctx, self.bounds); CGContextSetStrokeColorWithColor(ctx, _lineColor.CGColor); CGContextSetAlpha(ctx, _lineOpacity); CGContextSetLineWidth(ctx, _lineWidth/1.2); CGContextMoveToPoint(ctx, tOffsetX, self.bounds.size.height/2.0);//设置Path的起点 CGContextAddCurveToPoint(ctx, tOffsetX + tSpaceX, self.bounds.size.height/2.0 - tSpaceY, tOffsetX + tSpaceX*2, self.bounds.size.height/2.0 + tSpaceY, tOffsetX + tSpaceX*3, self.bounds.size.height/2.0); CGContextStrokePath(ctx); } - (void)dealloc { [_lineColor release]; [super dealloc]; } @end @interface PDFFreehandViewController () @property (nonatomic,assign) IBOutlet UIView *contentView; @property (nonatomic,assign) IBOutlet PDFFreehandDrawView *drawView; @property (nonatomic,assign) IBOutlet ColorView *colorView; @property (nonatomic,assign) IBOutlet UISlider *opacitySlider; @property (nonatomic,assign) IBOutlet UISlider *thicknessSlider; @property (nonatomic,assign) IBOutlet UILabel *thicknessMinLabel; @property (nonatomic,assign) IBOutlet UILabel *thicknessMaxLabel; @property (nonatomic,assign) IBOutlet UIButton *style1Button; @property (nonatomic,assign) IBOutlet UIButton *style2Button; @property (nonatomic,retain) UIView *styleSelectedView; @end @implementation PDFFreehandViewController #pragma mark - Init Methods - (void)dealloc { Block_release(_callback); [_styleSelectedView release]; [_color release]; [super dealloc]; } #pragma mark - UIViewController Methods - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. _styleSelectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; _styleSelectedView.backgroundColor = [UIColor lightGrayColor]; _styleSelectedView.layer.masksToBounds = YES; _styleSelectedView.layer.cornerRadius = 5; [self.view addSubview:_styleSelectedView]; __block __typeof(self) blockSelf = self; self.colorView.callback = ^(UIColor *color) { blockSelf.color = color; [blockSelf updateView]; if (blockSelf.callback) { blockSelf.callback(); } }; self.colorView.color = self.color; self.opacitySlider.value = self.opacity; [self updateView]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; self.preferredContentSize = CGSizeMake(300, self.contentView.bounds.size.height); UIButton *button = nil; if (self.style == 0) { button = self.style1Button; } else if (self.style == 1) { button = self.style2Button; } self.styleSelectedView.center = CGPointMake(button.center.x, button.frame.origin.y-self.styleSelectedView.bounds.size.height/2.0); } - (void)updateView { self.drawView.lineColor = self.color; self.drawView.lineOpacity = self.opacity/100; self.drawView.lineWidth = self.borderWidth; [self.drawView setNeedsDisplay]; if (self.style == 0) { self.thicknessSlider.minimumValue = 0.5; self.thicknessSlider.maximumValue = 10; self.thicknessMinLabel.text = @"0.5"; self.thicknessMaxLabel.text = @"10"; } else { self.thicknessSlider.minimumValue = 10; self.thicknessSlider.maximumValue = 30; self.thicknessMinLabel.text = @"10"; self.thicknessMaxLabel.text = @"30"; } self.thicknessSlider.value = self.borderWidth; } #pragma mark - Button Actions - (IBAction)buttonItemClicked_Style:(UIButton *)button { self.style = button.tag; self.styleSelectedView.center = CGPointMake(button.center.x, button.frame.origin.y-self.styleSelectedView.bounds.size.height/2.0); [self updateView]; if (self.callback) { self.callback(); } } - (IBAction)sliderValueChanged_Opacity:(UISlider *)slider { self.opacity = slider.value; [self updateView]; if (self.callback) { self.callback(); } } - (IBAction)sliderValueChanged_BorderWidth:(UISlider *)slider { self.borderWidth = slider.value; [self updateView]; if (self.callback) { self.callback(); } } #pragma mark - Public Methods - (void)showViewController:(UIViewController *)viewController inRect:(CGRect)rect { self.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *popVC = self.popoverPresentationController; popVC.delegate = self; popVC.sourceRect = rect; popVC.sourceView = viewController.view; [viewController presentViewController:self animated:YES completion:nil]; } #pragma mark - UIPopoverPresentationControllerDelegate - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { return UIModalPresentationNone; } @end