滑块动画时更新标签上的滑块值
Update slider value on label when slider animates
我在我的应用程序中使用滑块。我已经完成了所有设置,我面临的唯一问题是我想要的滑块值未更新的标签。看看我的代码。
我有一个按钮,单击它可以使滑块动画。我无法更新我的标签,因为它在整个动画中显示 0.0。
任何帮助将不胜感激。
- (IBAction)btnClicked:(id)sender
{
[UIView animateWithDuration:10 animations:^{
[self.slider setValue:0.0 animated:true];
} completion:^(BOOL finished) {
[UIView animateWithDuration:10 animations:^{
[self.slider setValue:100.0 animated:true];
}];
}];
}
这样试试:
- (IBAction)btnClicked:(id)sender
{
[self.slider setValue:0.0 animated:true];
[UIView animateWithDuration:10 animations:^{
[slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
} completion:^(BOOL finished) {
[UIView animateWithDuration:10 animations:^{
//[self.slider setValue:100.0 animated:true];
}];
}];
}
sliderChanged
方法是这样的:
- (void)sliderChanged:(UISlider *)slider {
self.label.text = [NSString stringWithFormat:@"%0.2f", slider.value];
}
您的代码无法正常工作,因为您尝试将最大值设置为滑块,以便它只是设置带有动画的最大值,仅此而已。试试我的代码,可能对你有帮助。
用这个改变你的 btnClicked
- (IBAction)btnClicked:(id)sender {
[self.slider setValue:0 animated:true];
[self setSliderValue:1];
}
添加这个 setSliderValue 方法
- (void)setSliderValue:(float)value {
[UIView animateWithDuration:0.2 animations:^{
[self.slider setValue:value animated:true];
} completion:^(BOOL finished) {
self.lblSliderValue.text = [NSString stringWithFormat:@"%0.0f", self.slider.value];
if (self.slider.value != self.slider.maximumValue) {
[self setSliderValue:value+1];
}
}];
}
希望对您有所帮助。
我在我的应用程序中使用滑块。我已经完成了所有设置,我面临的唯一问题是我想要的滑块值未更新的标签。看看我的代码。 我有一个按钮,单击它可以使滑块动画。我无法更新我的标签,因为它在整个动画中显示 0.0。 任何帮助将不胜感激。
- (IBAction)btnClicked:(id)sender
{
[UIView animateWithDuration:10 animations:^{
[self.slider setValue:0.0 animated:true];
} completion:^(BOOL finished) {
[UIView animateWithDuration:10 animations:^{
[self.slider setValue:100.0 animated:true];
}];
}];
}
这样试试:
- (IBAction)btnClicked:(id)sender
{
[self.slider setValue:0.0 animated:true];
[UIView animateWithDuration:10 animations:^{
[slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
} completion:^(BOOL finished) {
[UIView animateWithDuration:10 animations:^{
//[self.slider setValue:100.0 animated:true];
}];
}];
}
sliderChanged
方法是这样的:
- (void)sliderChanged:(UISlider *)slider {
self.label.text = [NSString stringWithFormat:@"%0.2f", slider.value];
}
您的代码无法正常工作,因为您尝试将最大值设置为滑块,以便它只是设置带有动画的最大值,仅此而已。试试我的代码,可能对你有帮助。
用这个改变你的 btnClicked
- (IBAction)btnClicked:(id)sender {
[self.slider setValue:0 animated:true];
[self setSliderValue:1];
}
添加这个 setSliderValue 方法
- (void)setSliderValue:(float)value {
[UIView animateWithDuration:0.2 animations:^{
[self.slider setValue:value animated:true];
} completion:^(BOOL finished) {
self.lblSliderValue.text = [NSString stringWithFormat:@"%0.0f", self.slider.value];
if (self.slider.value != self.slider.maximumValue) {
[self setSliderValue:value+1];
}
}];
}
希望对您有所帮助。