PDFSlider.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // PDFSlider.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFSlider.h"
  11. @interface PDFSlider ()
  12. @property (nonatomic,retain) UIImageView *valueView;
  13. @property (nonatomic,retain) UILabel *label;
  14. @property (nonatomic,assign) BOOL isTouchBegan;
  15. @end
  16. @implementation PDFSlider
  17. #pragma mark - Init Methods
  18. - (instancetype)initWithFrame:(CGRect)frame {
  19. if (self = [super initWithFrame:frame]) {
  20. _valueView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width-21, 0, 21, 69)];
  21. _valueView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  22. _valueView.image = [UIImage imageNamed:@"slidepage.png"];
  23. [self addSubview:_valueView];
  24. _label = [[UILabel alloc] init];
  25. _label.textAlignment = NSTextAlignmentCenter;
  26. _label.textColor = [UIColor blackColor];
  27. _label.backgroundColor = [UIColor whiteColor];
  28. _label.layer.cornerRadius = 2.0;
  29. _label.layer.borderWidth = 1.0;
  30. _label.layer.borderColor = [UIColor lightGrayColor].CGColor;
  31. _label.hidden = YES;
  32. [self addSubview:_label];
  33. }
  34. return self;
  35. }
  36. - (void)dealloc {
  37. [_label release];
  38. [_valueView release];
  39. [super dealloc];
  40. }
  41. #pragma mark - Public Methods
  42. - (void)reloadData {
  43. CGPoint center = self.valueView.center;
  44. NSInteger pageIndex = [self.pdfViewController.pdfView currentPageIndex];
  45. CGFloat height = CGRectGetHeight(self.valueView.frame);
  46. CGFloat pageHeight = CGRectGetHeight(self.frame)/[self.pdfViewController.pdfView.document pageCount];
  47. if (center.y >= pageIndex*pageHeight &&
  48. center.y <= (pageIndex+1)*pageHeight) {
  49. return;
  50. }
  51. center.y = pageIndex*pageHeight+pageHeight/2.0;
  52. center.y = MAX(height/2.0, center.y);
  53. center.y = MIN(center.y, CGRectGetHeight(self.frame)-height/2.0);
  54. self.valueView.center = center;
  55. }
  56. - (void)updateLabelFrame {
  57. CGPoint center = self.valueView.center;
  58. CGFloat height = CGRectGetHeight(self.valueView.frame);
  59. CGFloat pageHeight = (CGRectGetHeight(self.frame)-height)/[self.pdfViewController.pdfView.document pageCount];
  60. NSInteger pageIndex = (center.y-height/2.0)/pageHeight;
  61. pageIndex = MAX(0, pageIndex);
  62. pageIndex = MIN(pageIndex, [self.pdfViewController.pdfView.document pageCount]-1);
  63. self.label.text = [NSString stringWithFormat:@"%@",@(pageIndex+1)];
  64. [self.label sizeToFit];
  65. self.label.frame = CGRectMake(0, 0,
  66. CGRectGetWidth(self.label.frame)+20,
  67. CGRectGetHeight(self.label.frame)+10);
  68. self.label.center = CGPointMake(-CGRectGetWidth(self.label.frame)/2.0-30, center.y);
  69. }
  70. #pragma mark - Touch Methods
  71. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  72. UITouch *touch = [touches anyObject];
  73. CGPoint location = [touch locationInView:self];
  74. self.isTouchBegan = CGRectContainsPoint(self.valueView.frame, location);
  75. if (self.isTouchBegan) {
  76. self.label.hidden = NO;
  77. [self updateLabelFrame];
  78. }
  79. }
  80. - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  81. if (!self.isTouchBegan) {
  82. return;
  83. }
  84. UITouch *touch = [touches anyObject];
  85. CGPoint location = [touch locationInView:self];
  86. CGPoint center = self.valueView.center;
  87. if (location.y < CGRectGetHeight(self.valueView.frame)/2.0) {
  88. center.y = CGRectGetHeight(self.valueView.frame)/2.0;
  89. } else if (location.y > CGRectGetHeight(self.frame)-CGRectGetHeight(self.valueView.frame)/2.0) {
  90. center.y = CGRectGetHeight(self.frame)-CGRectGetHeight(self.valueView.frame)/2.0;
  91. } else {
  92. center.y = location.y;
  93. }
  94. self.valueView.center = center;
  95. [self updateLabelFrame];
  96. }
  97. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  98. if (!self.isTouchBegan) {
  99. return;
  100. }
  101. self.label.hidden = YES;
  102. NSInteger pageIndex = [self.label.text integerValue]-1;
  103. [self.pdfViewController.pdfView goToPageIndex:pageIndex animated:NO];
  104. }
  105. @end