我正在开发我的第一个iOS应用程序,其中包含一个UISlider
。我知道如何获得UISlider
被拖动时的值。但对于我的应用程序,我需要通过一次触摸来获得滑块的值;即如果我触摸UISlider
中的某处,UILabel
应显示其正确值。如何使用单键更改UISlider值?
有没有可能这样。任何教程或代码将非常有帮助。
我正在开发我的第一个iOS应用程序,其中包含一个UISlider
。我知道如何获得UISlider
被拖动时的值。但对于我的应用程序,我需要通过一次触摸来获得滑块的值;即如果我触摸UISlider
中的某处,UILabel
应显示其正确值。如何使用单键更改UISlider值?
有没有可能这样。任何教程或代码将非常有帮助。
请写下面的代码可以为你:)有帮助
这里tapCount是int
变量,在.h文件中
- (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
}
您可以使用下面的操作方法显示当前值UISlider
。
- (IBAction) sliderValueChanged:(UISlider *)sender {
yourLable.text = [NSString stringWithFormat:@"%.1f", [sender value]];
}
见UISlider
这些教程和示例:
本示例的第二个教程显示当前(最新)值UISlider
在UILable
中的示例,请参阅下图。
thanx您的帮助伞兵。但我不想找这个。我在这里找到另一个问题的答案。这就是我正在寻找http://stackoverflow.com/questions/2895122/iphoneprogramming-uislider-to-position-at-clicked-location – Midas
那么你想要什么? –
@Ravisankarvm哦好吧好吧现在我明白,使用点击或点击屏幕只是给改变滑块值的值... –
纠错@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;
}
从长远来看,我发现覆盖'UISlider'很脆弱,需要不断的维护才能跟上iOS的升级。我在https://github.com/arquebuse/TGPControls发布了更好的解决方案。它不会覆盖'UISlider',因此已经证明更加健壮。它还使用'CALayer'制作动画,这些动画非常流畅且完全可以自定义。 – SwiftArchitect
斯威夫特版本接受的答案,
有没有什么办法来计算UISlider上的水龙头数量.. – Midas
显示我编辑的答案 – iPatel
请同时发布swift –