【问题标题】:Animate UISlider smoothly平滑地为 UISlider 设置动画
【发布时间】:2012-12-03 09:20:39
【问题描述】:

我想为 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];
                 }];

只是动画第二个...

【问题讨论】:

    标签: ios animation user-interface uislider


    【解决方案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
            }];
        }];
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 1970-01-01
        • 2020-06-20
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        • 2019-07-24
        • 2016-06-27
        • 1970-01-01
        相关资源
        最近更新 更多