123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //
- // KSVideoImageSlider.m
- // KulexiuSchoolStudent
- //
- // Created by 王智 on 2023/11/8.
- //
- #import "KSVideoImageSlider.h"
- #define BOUND(VALUE, UPPER, LOWER) MIN(MAX(VALUE, LOWER), UPPER)
- @interface KSVideoImageSlider ()
- {
- UIImageView *_knobView;
- float _knobWidth;
- float _knobHeight;
- float _useableTrackLength;
- CGPoint _previousTouchPoint;
- }
- @end
- @implementation KSVideoImageSlider
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- _maximumValue = 10.0;
- _minimumValue = 0.0;
- _previousTouchPoint = CGPointMake(0, 0);
- // _knobValue = 0.0;
- [self createLayerView:frame];
- [self createTheknob];
- [self setKnodFrames];
- }
- return self;
- }
- - (void)createLayerView:(CGRect)frame {
- UIView *layerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
- layerView.backgroundColor = [UIColor blackColor];
- layerView.alpha = 0.32;
- layerView.userInteractionEnabled = NO;
- [self addSubview:layerView];
- }
- #pragma mark --------PublicMethod
- -(void)createTheImageWall {
- for (int i=0; i<self.maximumValue; i++) {
- UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*(self.frame.size.width/self.maximumValue), 2, self.frame.size.width/self.maximumValue, self.frame.size.height-4)];
- UIImage *image = self.imageArray[i];
- [imageView setImage:image];
- [self addSubview:imageView];
- [self sendSubviewToBack:imageView];
- }
- }
- - (void)updateTheKnobImage:(UIImage*)image{
- [_knobView setImage:image];
- }
- - (void)moveKnobWithSelectedValue:(float)value{
- [self setKnobValue:value];
- [self setKnodFrames];
- }
- #pragma mark --------PrivateMethod
- - (void)createTheknob{
- if (_knobView == nil) {
- _knobView = [[UIImageView alloc]init];
- _knobView.layer.cornerRadius = 10.0f;
- _knobView.layer.borderColor = HexRGB(0x279FFE).CGColor;
- _knobView.layer.borderWidth = 4.0f;
- _knobView.layer.masksToBounds = YES;
- _knobView.contentMode = UIViewContentModeScaleAspectFill;
- [self addSubview:_knobView];
- [self bringSubviewToFront:_knobView];
- }
- }
- - (void) setKnodFrames {
- _knobWidth = self.bounds.size.height / 9 * 16;
- _knobHeight = self.bounds.size.height;
- _useableTrackLength = self.bounds.size.width - _knobWidth;
-
- float knobCentre = [self positionForValue:self.knobValue];
- _knobView.frame = CGRectMake(knobCentre - _knobWidth / 2, 0, _knobWidth, _knobHeight);
- }
- - (float)positionForValue:(float)value
- {
- return _useableTrackLength * (value - _minimumValue) /
- (_maximumValue - _minimumValue) + (_knobWidth / 2);
- }
- #pragma mark -----------TouchHandle
- - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
- CGPoint touchPoint = [touch locationInView:self];
-
- // 1. determine by how much the user has dragged
- float delta = touchPoint.x - _previousTouchPoint.x;
- float valueDelta = (_maximumValue - _minimumValue) * delta / _useableTrackLength;
-
- _previousTouchPoint = touchPoint;
-
- _knobValue += valueDelta;
- _knobValue = BOUND(_knobValue, _maximumValue, _minimumValue);
- [CATransaction begin];
- [CATransaction setDisableActions:YES] ;
-
- [self setKnodFrames];
-
- [CATransaction commit];
- [self sendActionsForControlEvents:UIControlEventValueChanged];
- return YES;
- }
- - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
- CGPoint touchPoint = [touch locationInView:self];
-
- // 1. determine by how much the user has dragged
- float delta = touchPoint.x - _previousTouchPoint.x;
- float valueDelta = (_maximumValue - _minimumValue) * delta / _useableTrackLength;
-
- _previousTouchPoint = touchPoint;
-
- _knobValue += valueDelta;
- _knobValue = BOUND(_knobValue, _maximumValue, _minimumValue);
- [CATransaction begin];
- [CATransaction setDisableActions:YES] ;
-
- [self setKnodFrames];
-
- [CATransaction commit];
- [self sendActionsForControlEvents:UIControlEventValueChanged];
- return YES;
- }
- - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
-
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|