KSVideoImageSlider.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // KSVideoImageSlider.m
  3. // KulexiuSchoolStudent
  4. //
  5. // Created by 王智 on 2023/11/8.
  6. //
  7. #import "KSVideoImageSlider.h"
  8. #define BOUND(VALUE, UPPER, LOWER) MIN(MAX(VALUE, LOWER), UPPER)
  9. @interface KSVideoImageSlider ()
  10. {
  11. UIImageView *_knobView;
  12. float _knobWidth;
  13. float _knobHeight;
  14. float _useableTrackLength;
  15. CGPoint _previousTouchPoint;
  16. }
  17. @end
  18. @implementation KSVideoImageSlider
  19. - (id)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. // Initialization code
  24. _maximumValue = 10.0;
  25. _minimumValue = 0.0;
  26. _previousTouchPoint = CGPointMake(0, 0);
  27. // _knobValue = 0.0;
  28. [self createLayerView:frame];
  29. [self createTheknob];
  30. [self setKnodFrames];
  31. }
  32. return self;
  33. }
  34. - (void)createLayerView:(CGRect)frame {
  35. UIView *layerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  36. layerView.backgroundColor = [UIColor blackColor];
  37. layerView.alpha = 0.32;
  38. layerView.userInteractionEnabled = NO;
  39. [self addSubview:layerView];
  40. }
  41. #pragma mark --------PublicMethod
  42. -(void)createTheImageWall {
  43. for (int i=0; i<self.maximumValue; i++) {
  44. 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)];
  45. UIImage *image = self.imageArray[i];
  46. [imageView setImage:image];
  47. [self addSubview:imageView];
  48. [self sendSubviewToBack:imageView];
  49. }
  50. }
  51. - (void)updateTheKnobImage:(UIImage*)image{
  52. [_knobView setImage:image];
  53. }
  54. - (void)moveKnobWithSelectedValue:(float)value{
  55. [self setKnobValue:value];
  56. [self setKnodFrames];
  57. }
  58. #pragma mark --------PrivateMethod
  59. - (void)createTheknob{
  60. if (_knobView == nil) {
  61. _knobView = [[UIImageView alloc]init];
  62. _knobView.layer.cornerRadius = 10.0f;
  63. _knobView.layer.borderColor = HexRGB(0x279FFE).CGColor;
  64. _knobView.layer.borderWidth = 4.0f;
  65. _knobView.layer.masksToBounds = YES;
  66. _knobView.contentMode = UIViewContentModeScaleAspectFill;
  67. [self addSubview:_knobView];
  68. [self bringSubviewToFront:_knobView];
  69. }
  70. }
  71. - (void) setKnodFrames {
  72. _knobWidth = self.bounds.size.height / 9 * 16;
  73. _knobHeight = self.bounds.size.height;
  74. _useableTrackLength = self.bounds.size.width - _knobWidth;
  75. float knobCentre = [self positionForValue:self.knobValue];
  76. _knobView.frame = CGRectMake(knobCentre - _knobWidth / 2, 0, _knobWidth, _knobHeight);
  77. }
  78. - (float)positionForValue:(float)value
  79. {
  80. return _useableTrackLength * (value - _minimumValue) /
  81. (_maximumValue - _minimumValue) + (_knobWidth / 2);
  82. }
  83. #pragma mark -----------TouchHandle
  84. - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
  85. CGPoint touchPoint = [touch locationInView:self];
  86. // 1. determine by how much the user has dragged
  87. float delta = touchPoint.x - _previousTouchPoint.x;
  88. float valueDelta = (_maximumValue - _minimumValue) * delta / _useableTrackLength;
  89. _previousTouchPoint = touchPoint;
  90. _knobValue += valueDelta;
  91. _knobValue = BOUND(_knobValue, _maximumValue, _minimumValue);
  92. [CATransaction begin];
  93. [CATransaction setDisableActions:YES] ;
  94. [self setKnodFrames];
  95. [CATransaction commit];
  96. [self sendActionsForControlEvents:UIControlEventValueChanged];
  97. return YES;
  98. }
  99. - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
  100. CGPoint touchPoint = [touch locationInView:self];
  101. // 1. determine by how much the user has dragged
  102. float delta = touchPoint.x - _previousTouchPoint.x;
  103. float valueDelta = (_maximumValue - _minimumValue) * delta / _useableTrackLength;
  104. _previousTouchPoint = touchPoint;
  105. _knobValue += valueDelta;
  106. _knobValue = BOUND(_knobValue, _maximumValue, _minimumValue);
  107. [CATransaction begin];
  108. [CATransaction setDisableActions:YES] ;
  109. [self setKnodFrames];
  110. [CATransaction commit];
  111. [self sendActionsForControlEvents:UIControlEventValueChanged];
  112. return YES;
  113. }
  114. - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
  115. }
  116. /*
  117. // Only override drawRect: if you perform custom drawing.
  118. // An empty implementation adversely affects performance during animation.
  119. - (void)drawRect:(CGRect)rect {
  120. // Drawing code
  121. }
  122. */
  123. @end