// // PDFKTslideSizeView.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 "PDFKTSlideView.h" @implementation PDFKTSlideView { UIView *_valueView; UILabel *_valueLabel; UILabel *_valueLabelonView; CGFloat _beginValue; UIImageView *_backView; CGFloat _valueViewWidth; } @synthesize slideDelegate = _slideDelegate; @synthesize curValue = _curValue; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code UIImage *backImg = [UIImage imageNamed:@"size_bar.png"]; _backView = [[UIImageView alloc]initWithImage:backImg]; _backView.frame = CGRectMake(0, -70, backImg.size.width, backImg.size.height); CGPoint backViewCenter = _backView.center; backViewCenter.x = self.bounds.size.width / 2; _backView.center = backViewCenter; // _backView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin; [self addSubview:_backView]; // _backView.hidden = YES; _valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,56,42)]; _valueLabel.textAlignment = NSTextAlignmentCenter; _valueLabel.backgroundColor = [UIColor clearColor]; _valueLabel.text = @"1"; _valueLabel.center = CGPointMake(_backView.bounds.size.width - _valueLabel.bounds.size.width / 2, _valueLabel.center.y); [_backView addSubview:_valueLabel]; UIImage *sizeImg = [UIImage imageNamed:@"size_scale.png"]; UIImageView *sizeBack = [[UIImageView alloc] initWithImage:sizeImg]; sizeBack.frame = CGRectMake(0, 0, sizeImg.size.width, sizeImg.size.height); _valueView = [[UIView alloc] initWithFrame:CGRectMake(7, 5, sizeImg.size.width, sizeImg.size.height)]; [_valueView addSubview:sizeBack]; _valueView.clipsToBounds = YES; [sizeBack release]; _valueViewWidth = sizeImg.size.width; [_backView addSubview:_valueView]; _backView.hidden =YES; _valueLabelonView = [[UILabel alloc] initWithFrame:self.bounds]; _valueLabelonView.textAlignment = NSTextAlignmentCenter; _valueLabelonView.text = @"1"; _valueLabelonView.textColor = [UIColor whiteColor]; _valueLabelonView.backgroundColor = [UIColor clearColor]; [self addSubview:_valueLabelonView]; _minValue = 1; _maxValue = 99; } return self; } - (void)layoutSubviews{ [super layoutSubviews]; CGPoint center = _backView.center; center.x = self.frame.size.width/2.0; _backView.center = center; } - (void)dealloc { [_valueView release]; [_valueLabel release]; [_backView release]; [_valueLabelonView release]; _slideDelegate = nil; [super dealloc]; } - (void)setCurValue:(CGFloat)curValue { _curValue = curValue; _valueLabelonView.text = [NSString stringWithFormat:@"%.f",curValue]; _valueLabel.text = [NSString stringWithFormat:@"%.f",curValue]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; _beginValue = location.x; int curValue = [_valueLabel.text intValue]; if (curValue > _maxValue) curValue = _maxValue; if (curValue < _minValue) curValue = _minValue; CGRect frame = _valueView.frame; frame.size.width = _valueViewWidth * (curValue - _minValue + 1) / (_maxValue - _minValue +1); [_valueView setFrame:frame]; _backView.hidden = NO; [self.slideDelegate slideDidBegin:self]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // 检查point是否改变 CGPoint cPoint = [[touches anyObject] locationInView:self]; CGPoint pPoint = [[touches anyObject] previousLocationInView:self]; if (CGPointEqualToPoint(cPoint, pPoint)) { return; } if (fabs(cPoint.x - pPoint.x) < 0.5) { return; } UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; int curValue = [_valueLabel.text intValue]; if (location.x > _beginValue) curValue++; else curValue --; if (curValue > _maxValue) curValue = _maxValue; if (curValue < _minValue) curValue = _minValue; _beginValue = location.x; _valueLabel.text = [NSString stringWithFormat:@"%d",curValue]; _valueLabelonView.text = [NSString stringWithFormat:@"%d",curValue]; CGRect frame = _valueView.frame; frame.size.width = _valueViewWidth * (curValue - _minValue + 1) / (_maxValue - _minValue +1); [_valueView setFrame:frame]; _backView.hidden = NO; if (_slideDelegate && [_slideDelegate respondsToSelector:@selector(slideView:updateValue:)]) [_slideDelegate slideView:self updateValue:[_valueLabel.text floatValue]]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; _beginValue = location.x; _backView.hidden = YES; [self.slideDelegate slideDidEnd:self]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ [self touchesEnded:touches withEvent:event]; } @end