PDFKTSlideView.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // PDFKTslideSizeView.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 "PDFKTSlideView.h"
  11. @implementation PDFKTSlideView
  12. {
  13. UIView *_valueView;
  14. UILabel *_valueLabel;
  15. UILabel *_valueLabelonView;
  16. CGFloat _beginValue;
  17. UIImageView *_backView;
  18. CGFloat _valueViewWidth;
  19. }
  20. @synthesize slideDelegate = _slideDelegate;
  21. @synthesize curValue = _curValue;
  22. - (id)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. // Initialization code
  27. UIImage *backImg = [UIImage imageNamed:@"size_bar.png"];
  28. _backView = [[UIImageView alloc]initWithImage:backImg];
  29. _backView.frame = CGRectMake(0, -70, backImg.size.width, backImg.size.height);
  30. CGPoint backViewCenter = _backView.center;
  31. backViewCenter.x = self.bounds.size.width / 2;
  32. _backView.center = backViewCenter;
  33. // _backView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;
  34. [self addSubview:_backView];
  35. // _backView.hidden = YES;
  36. _valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,56,42)];
  37. _valueLabel.textAlignment = NSTextAlignmentCenter;
  38. _valueLabel.backgroundColor = [UIColor clearColor];
  39. _valueLabel.text = @"1";
  40. _valueLabel.center = CGPointMake(_backView.bounds.size.width - _valueLabel.bounds.size.width / 2, _valueLabel.center.y);
  41. [_backView addSubview:_valueLabel];
  42. UIImage *sizeImg = [UIImage imageNamed:@"size_scale.png"];
  43. UIImageView *sizeBack = [[UIImageView alloc] initWithImage:sizeImg];
  44. sizeBack.frame = CGRectMake(0, 0, sizeImg.size.width, sizeImg.size.height);
  45. _valueView = [[UIView alloc] initWithFrame:CGRectMake(7, 5, sizeImg.size.width, sizeImg.size.height)];
  46. [_valueView addSubview:sizeBack];
  47. _valueView.clipsToBounds = YES;
  48. [sizeBack release];
  49. _valueViewWidth = sizeImg.size.width;
  50. [_backView addSubview:_valueView];
  51. _backView.hidden =YES;
  52. _valueLabelonView = [[UILabel alloc] initWithFrame:self.bounds];
  53. _valueLabelonView.textAlignment = NSTextAlignmentCenter;
  54. _valueLabelonView.text = @"1";
  55. _valueLabelonView.textColor = [UIColor whiteColor];
  56. _valueLabelonView.backgroundColor = [UIColor clearColor];
  57. [self addSubview:_valueLabelonView];
  58. _minValue = 1;
  59. _maxValue = 99;
  60. }
  61. return self;
  62. }
  63. - (void)layoutSubviews{
  64. [super layoutSubviews];
  65. CGPoint center = _backView.center;
  66. center.x = self.frame.size.width/2.0;
  67. _backView.center = center;
  68. }
  69. - (void)dealloc
  70. {
  71. [_valueView release];
  72. [_valueLabel release];
  73. [_backView release];
  74. [_valueLabelonView release];
  75. _slideDelegate = nil;
  76. [super dealloc];
  77. }
  78. - (void)setCurValue:(CGFloat)curValue
  79. {
  80. _curValue = curValue;
  81. _valueLabelonView.text = [NSString stringWithFormat:@"%.f",curValue];
  82. _valueLabel.text = [NSString stringWithFormat:@"%.f",curValue];
  83. }
  84. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  85. {
  86. UITouch *touch = [touches anyObject];
  87. CGPoint location = [touch locationInView:self];
  88. _beginValue = location.x;
  89. int curValue = [_valueLabel.text intValue];
  90. if (curValue > _maxValue) curValue = _maxValue;
  91. if (curValue < _minValue) curValue = _minValue;
  92. CGRect frame = _valueView.frame;
  93. frame.size.width = _valueViewWidth * (curValue - _minValue + 1) / (_maxValue - _minValue +1);
  94. [_valueView setFrame:frame];
  95. _backView.hidden = NO;
  96. [self.slideDelegate slideDidBegin:self];
  97. }
  98. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  99. {
  100. // 检查point是否改变
  101. CGPoint cPoint = [[touches anyObject] locationInView:self];
  102. CGPoint pPoint = [[touches anyObject] previousLocationInView:self];
  103. if (CGPointEqualToPoint(cPoint, pPoint)) {
  104. return;
  105. }
  106. if (fabs(cPoint.x - pPoint.x) < 0.5) {
  107. return;
  108. }
  109. UITouch *touch = [touches anyObject];
  110. CGPoint location = [touch locationInView:self];
  111. int curValue = [_valueLabel.text intValue];
  112. if (location.x > _beginValue) curValue++;
  113. else curValue --;
  114. if (curValue > _maxValue) curValue = _maxValue;
  115. if (curValue < _minValue) curValue = _minValue;
  116. _beginValue = location.x;
  117. _valueLabel.text = [NSString stringWithFormat:@"%d",curValue];
  118. _valueLabelonView.text = [NSString stringWithFormat:@"%d",curValue];
  119. CGRect frame = _valueView.frame;
  120. frame.size.width = _valueViewWidth * (curValue - _minValue + 1) / (_maxValue - _minValue +1);
  121. [_valueView setFrame:frame];
  122. _backView.hidden = NO;
  123. if (_slideDelegate && [_slideDelegate respondsToSelector:@selector(slideView:updateValue:)])
  124. [_slideDelegate slideView:self updateValue:[_valueLabel.text floatValue]];
  125. }
  126. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  127. {
  128. UITouch *touch = [touches anyObject];
  129. CGPoint location = [touch locationInView:self];
  130. _beginValue = location.x;
  131. _backView.hidden = YES;
  132. [self.slideDelegate slideDidEnd:self];
  133. }
  134. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  135. [self touchesEnded:touches withEvent:event];
  136. }
  137. @end