动画UISlider平滑

时间:2012-12-15 17:35:23

标签: ios animation user-interface uislider

我想为UISlider设置动画,例如0.25到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];
                 }];

只是动画第二个......

2 个答案:

答案 0 :(得分:14)

您需要通过将第二个动画放在第一个动画的完成块中来链接动画:

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

答案 1 :(得分:2)

对于rdelmar的回答,

Swift 2.2版本

@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)