// // PDFSlider.m // 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 "PDFSlider.h" @interface PDFSlider () @property (nonatomic,retain) UIImageView *valueView; @property (nonatomic,retain) UILabel *label; @property (nonatomic,assign) BOOL isTouchBegan; @end @implementation PDFSlider #pragma mark - Init Methods - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _valueView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width-21, 0, 21, 69)]; _valueView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; _valueView.image = [UIImage imageNamed:@"slidepage.png"]; [self addSubview:_valueView]; _label = [[UILabel alloc] init]; _label.textAlignment = NSTextAlignmentCenter; _label.textColor = [UIColor blackColor]; _label.backgroundColor = [UIColor whiteColor]; _label.layer.cornerRadius = 2.0; _label.layer.borderWidth = 1.0; _label.layer.borderColor = [UIColor lightGrayColor].CGColor; _label.hidden = YES; [self addSubview:_label]; } return self; } - (void)dealloc { [_label release]; [_valueView release]; [super dealloc]; } #pragma mark - Public Methods - (void)reloadData { CGPoint center = self.valueView.center; NSInteger pageIndex = [self.pdfViewController.pdfView currentPageIndex]; CGFloat height = CGRectGetHeight(self.valueView.frame); CGFloat pageHeight = CGRectGetHeight(self.frame)/[self.pdfViewController.pdfView.document pageCount]; if (center.y >= pageIndex*pageHeight && center.y <= (pageIndex+1)*pageHeight) { return; } center.y = pageIndex*pageHeight+pageHeight/2.0; center.y = MAX(height/2.0, center.y); center.y = MIN(center.y, CGRectGetHeight(self.frame)-height/2.0); self.valueView.center = center; } - (void)updateLabelFrame { CGPoint center = self.valueView.center; CGFloat height = CGRectGetHeight(self.valueView.frame); CGFloat pageHeight = (CGRectGetHeight(self.frame)-height)/[self.pdfViewController.pdfView.document pageCount]; NSInteger pageIndex = (center.y-height/2.0)/pageHeight; pageIndex = MAX(0, pageIndex); pageIndex = MIN(pageIndex, [self.pdfViewController.pdfView.document pageCount]-1); self.label.text = [NSString stringWithFormat:@"%@",@(pageIndex+1)]; [self.label sizeToFit]; self.label.frame = CGRectMake(0, 0, CGRectGetWidth(self.label.frame)+20, CGRectGetHeight(self.label.frame)+10); self.label.center = CGPointMake(-CGRectGetWidth(self.label.frame)/2.0-30, center.y); } #pragma mark - Touch Methods - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; self.isTouchBegan = CGRectContainsPoint(self.valueView.frame, location); if (self.isTouchBegan) { self.label.hidden = NO; [self updateLabelFrame]; } } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (!self.isTouchBegan) { return; } UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; CGPoint center = self.valueView.center; if (location.y < CGRectGetHeight(self.valueView.frame)/2.0) { center.y = CGRectGetHeight(self.valueView.frame)/2.0; } else if (location.y > CGRectGetHeight(self.frame)-CGRectGetHeight(self.valueView.frame)/2.0) { center.y = CGRectGetHeight(self.frame)-CGRectGetHeight(self.valueView.frame)/2.0; } else { center.y = location.y; } self.valueView.center = center; [self updateLabelFrame]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (!self.isTouchBegan) { return; } self.label.hidden = YES; NSInteger pageIndex = [self.label.text integerValue]-1; [self.pdfViewController.pdfView goToPageIndex:pageIndex animated:NO]; } @end