如何获得UISlider拇指位置

时间:2017-07-28 11:03:33

标签: objective-c vlc

如何知道UISlider拇指位置?
当我按下按钮时,我想在当前滑块拇指位置上添加一个标志 如何获取当前拇指位置以便我可以添加标志?

这是我的同步代码:

 _positionSlider.value = _mediaplayer.position;

这是滑块的样子:

look like this image

2 个答案:

答案 0 :(得分:1)

试试这个, 首先创建UIImageView

@interface ViewController ()
{
    UIImageView *imageView;
}

并在viewDiload()函数

中写下以下代码
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];

并添加此滑块事件功能

-(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);
 }

答案 1 :(得分:1)

快速版本:

@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)
}