Thumb image does not move to the edge of UISlider

后端 未结 9 1170
有刺的猬
有刺的猬 2021-02-13 02:14

Thumb image does not move to the edge even when it\'s value is max or min.

Does anyone know how to make it move all the way to the edge of the slider?

9条回答
  •  情话喂你
    2021-02-13 02:59

    So my solution to this problem was to make the colour of the slider Min Track and Max Track transparent and then draw a new track in the initWithCoder and update the size of the track in the continueTrackingWithTouch.

    -(AnalogueSlider *)initWithCoder:(NSCoder *)coder :(NSArray *)labels {
        self = [super initWithCoder:coder];
    
        trackRect = [self trackRectForBounds:self.bounds];
        thumbRect = [self thumbRectForBounds:self.bounds trackRect:trackRect value:self.value];
    
        // drawn a new track
        leftTrack = [[UIView alloc] initWithFrame:CGRectMake(trackRect.origin.x+thumbRect.size.width/2.0, trackRect.origin.y, thumbRect.origin.x-thumbRect.size.width, trackRect.size.height)];
        leftTrack.backgroundColor = [UIColor blueColor];
        [self insertSubview:leftTrack belowSubview:self];
    
        rightTrack = [[UIView alloc] initWithFrame:CGRectMake(thumbRect.origin.x+thumbRect.size.width/2.0, trackRect.origin.y, trackRect.size.width-thumbRect.origin.x, trackRect.size.height)];
        rightTrack.backgroundColor = [UIColor grayColor];
        [self insertSubview:rightTrack belowSubview:self];
    
        return self;
    }
    
    -(BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
        thumbRect = [self thumbRectForBounds:self.bounds trackRect:trackRect value:self.value];
        leftTrack.frame = CGRectMake(trackRect.origin.x+thumbRect.size.width/2.0, trackRect.origin.y, thumbRect.origin.x-thumbRect.size.width, trackRect.size.height);
        rightTrack.frame = CGRectMake(thumbRect.origin.x+thumbRect.size.width/2.0, trackRect.origin.y, trackRect.size.width-thumbRect.origin.x, trackRect.size.height);
    
        return [super continueTrackingWithTouch:touch withEvent:event];
    }
    

    This allows the thumb image to travel further than the track so the centre of the thumb is at the end of the track.

    And at the end:

提交回复
热议问题