123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- //
- // 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 () <UIPopoverPresentationControllerDelegate>
- @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
|