动画UISlider顺利(Animate UISlider smoothly)

2019-08-04 04:04发布

我想为如0.25 UISlider动画到0.75和背部。 这应该表现出什么是应该做的用户。

我想这:

[self incrementCounter:[NSNumber numberWithInt:0]];


-(void) incrementCounter:(NSNumber *)i {
    [Slider setValue:[i floatValue]/1000];
    [self performSelector:@selector(incrementCounter:) withObject:[NSNumber numberWithInt:i.intValue+1] afterDelay:0.001];
}

但那不是那么顺利......我可以用这个转变?

[Slider setValue:1 animated:YES];

是快速...

[UIView animateWithDuration:2.0
                 animations:^{
                     [Slider setValue:0.2];
                 }];
[UIView animateWithDuration:2.0
                 animations:^{
                     [Slider setValue:0.5];
                 }];

只是动画第二个...

Answer 1:

你需要通过链条把第二动画第一完成块中的动画:

-(IBAction)animateSlider:(id)sender {

    [UIView animateWithDuration:2 animations:^{
        [self.slider setValue:.75];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2 animations:^{
            [self.slider setValue:0.25];//or `[self.slider setValue:(.75)-(.5*finished)];` if you want to be safe
        }];
    }];
}


Answer 2:

雨燕2.2版本 rdelmar的答案

@IBOutlet weak var slider: UISlider!

func animateSlider(sender: AnyObject){
    UIView.animateWithDuration(2, animations:  {() in
        self.slider.setValue(0.75, animated: true)
        }, completion:{(Bool)  in
            UIView.animateWithDuration(2, animations: {() in
                self.slider.setValue(0.25, animated: true)
            })
    })
}

并调用函数传递UISlider在参数

animateSlider(self.slider)


文章来源: Animate UISlider smoothly