Jinsuk Oh
Jinsuk Oh

Reputation: 63

How to get UISlider thumb position

How to know UISlider thumb position?
When I press a button I want to add a flag on the current slider thumb position.
How can I acquire the current thumb position so I can add the flag?

This is my synchronization code:

 _positionSlider.value = _mediaplayer.position;

This is how the slider should look like:

look like this image

Upvotes: 4

Views: 5185

Answers (2)

SoftDesigner
SoftDesigner

Reputation: 5854

Swift version:

@objc func sliderChanged() {
    let trackRect = slider.trackRect(forBounds: slider.bounds)
    let thumbRect = slider.thumbRect(forBounds: slider.bounds, trackRect: trackRect, value: slider.value)

    arrowLeadingConstraint.update(offset: thumbRect.midX)
}

Upvotes: 7

Subramani
Subramani

Reputation: 477

Try This, First create UIImageView

@interface ViewController ()
{
    UIImageView *imageView;
}

and write this below code in viewDiload() Function

imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.slider.frame.origin.x, self.slider.frame.origin.y-20, 20, 10)];
imageView.image = [UIImage imageNamed:@"navigateImage.png"];
[self.view addSubview:imageView];

[self.slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];

and add this slider event function

-(void)sliderValueChanged:(UISlider *)slider{

    CGRect trackRect = [self.slider trackRectForBounds:self.slider.bounds];
    CGRect thumbRect = [self.slider thumbRectForBounds:self.slider.bounds
                                             trackRect:trackRect
                                                 value:self.slider.value];

    CGRect r = [imageView frame];
    r.origin.x = thumbRect.origin.x;
    [imageView setFrame:r];

    NSLog(@"%f",thumbRect.origin.x);
 }

Upvotes: 9

Related Questions