Search code examples
iphoneiosuislider

Move label with UISlider


I have a requirement that i have to move value on label with UISlider. I tried with this code

[slider addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside];

- (void)dragMoving:(UIControl *)c withEvent:ev {  
    UITouch *touch = [[ev allTouches] anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
     sliderLabe.frame = CGRectMake(touchPoint.x,250, 270, 40);
}

sliderLabel is just below the slider, But it is giving me strange result when i drag faster but it works fine if i move mu slider slowly.


Solution

  • While slider is moving it's also calculating it's new value and calling valueChanged. This could interfere with your extra UIControlEventTouchDragInside handler.

    If slider.continuous is set to YES you could try moving youre label-moving-code into valueChanged handler.

    Of course touch location is not known in this method so it needs to be calculated from slider's other properties: frame.origin.x, frame.size.width, minimumValue, maximumValue and value