10

我正在开发我的第一个 iOS 应用程序,其中包含一个UISlider. 我知道如何在UISlider拖动时获取值。但是对于我的应用程序,我需要一键获取滑块的值;即,如果我触摸 中的某处UISlider, aUILabel应该显示其正确值。

有没有可能采用这种方式。任何教程或代码都会非常有帮助。

4

4 回答 4

20

这里tapCount是在.h 文件int中声明的变量

- (void)viewDidLoad
{
  tapCount = 0; // Count tap on Slider 

 UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
    [slider addGestureRecognizer:gr];
    
}

 - (void)sliderTapped:(UIGestureRecognizer *)g 
    {
/////////////// For TapCount////////////

        tapCount = tapCount + 1;
        NSLog(@"Tap Count -- %d",tapCount);

/////////////// For TapCount////////////

        UISlider* s = (UISlider*)g.view;
        if (s.highlighted)
            return; // tap on thumb, let slider deal with it
        CGPoint pt = [g locationInView: s];
        CGFloat percentage = pt.x / s.bounds.size.width;
        CGFloat delta = percentage * (s.maximumValue - s.minimumValue);
        CGFloat value = s.minimumValue + delta;
        [s setValue:value animated:YES];
        
        NSString *str=[NSString stringWithFormat:@"%.f",[self.slider value]];
        self.lbl.text=str;
    }

对于 Swift 用户

override func viewDidLoad()
{
    super.viewDidLoad()

    tapCount = 0
    let gr = UITapGestureRecognizer(target: self, action: #selector(self.sliderTapped(_:)))
    slider.addGestureRecognizer(gr)

}

func sliderTapped(_ g: UIGestureRecognizer) {
    
    /////////////// For TapCount////////////
    tapCount = tapCount + 1
    print("Tap Count -- \(tapCount)")
    /////////////// For TapCount////////////
    
    let s: UISlider? = (g.view as? UISlider)
    if (s?.isHighlighted)! {
        return
    }
    
    // tap on thumb, let slider deal with it
    let pt: CGPoint = g.location(in: s)
    let percentage = pt.x / (s?.bounds.size.width)!
    let delta = Float(percentage) * Float((s?.maximumValue)! - (s?.minimumValue)!)
    let value = (s?.minimumValue)! + delta
    s?.setValue(Float(value), animated: true)
    let str = String(format: "%.f", slider.value)
    lbl.text = str
}
于 2013-01-16T10:56:59.713 回答
10

更正@ipatel:先前答案中的数学不完整,并且由于缺少拇指宽度考虑而导致抖动。

如果您选择子类化,它应该是这样的UISlider

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super beginTrackingWithTouch:touch withEvent:event];
    CGPoint touchLocation = [touch locationInView:self];
    CGFloat value = self.minimumValue + (self.maximumValue - self.minimumValue) *
        ((touchLocation.x - self.currentThumbImage.size.width/2) /
        (self.frame.size.width-self.currentThumbImage.size.width));
    [self setValue:value animated:YES];

    return YES;
}
于 2013-10-19T04:04:58.920 回答
2

接受答案的 Swift 版本,

@IBAction func sliderTappedAction(sender: UITapGestureRecognizer)
{
    if let slider = sender.view as? UISlider {

        if slider.highlighted { return }

        let point = sender.locationInView(slider)
        let percentage = Float(point.x / CGRectGetWidth(slider.bounds))
        let delta = percentage * (slider.maximumValue - slider.minimumValue)
        let value = slider.minimumValue + delta
        slider.setValue(value, animated: true)
    }
}
于 2016-01-14T12:14:34.453 回答
0

UISlider您可以使用其操作方法显示当前值,如下所示。

- (IBAction) sliderValueChanged:(UISlider *)sender {  
    yourLable.text = [NSString stringWithFormat:@"%.1f", [sender value]];  
} 

请参阅以下教程和示例UISlider

  1. UISlider-tutorial-example-how-to-use-slider-in-iphone-sdk-xcode
  2. iphone-slider-control-uislider-control-tutorial

第二个教程带有显示当前(最新)值的示例UISliderUILable 请参见下图。

在此处输入图像描述

于 2013-01-16T10:47:38.250 回答