在动画制作时UISlider不受欢迎的行为

时间:2016-06-08 06:12:43

标签: ios objective-c cocoa-touch uislider

我在我的应用程序中使用UISlider,我为滑块添加了最大和最小图像。每件事情都很好,但请看下面的图片。当我点击按钮时,动画开始,标签上的值发生变化但是滑块上的动画直接跳到6,你可以看到它,当滑块达到最大值(100)时,滑块最小图像(橙色)会发生同样的事情。颜色)消失,标签继续显示值97..98..99..100。enter image description here

以下是参考代码段:

 - (void)viewDidLoad
 {
  [super viewDidLoad];
  UIImage *sliderMinimum = [[UIImage imageNamed:@"slider_max"]    stretchableImageWithLeftCapWidth:14 topCapHeight:0];
  [self.slider setMinimumTrackImage:sliderMinimum forState:UIControlStateNormal];
  UIImage *sliderMaximum = [[UIImage imageNamed:@"slider_min"] stretchableImageWithLeftCapWidth:14 topCapHeight:0];
  [self.slider setMaximumTrackImage:sliderMaximum forState:UIControlStateNormal];
 }

- (IBAction)btnClicked:(id)sender
{
[self.slider setValue:0 animated:true];
[self setSliderValue:2];
}

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

1 个答案:

答案 0 :(得分:0)

试试这个:

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

在roundValue函数中设置值:

- (void)roundValue{

    [slider setValue:((int)((slider.value + 0) / 1) * 1) animated:NO];

   }