2012-12-15 112 views
7

我想動畫用於例如一個UISlider 0.25到0.75並返回。這應該顯示用戶該做什麼。動畫UISlider順利

我想這:

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

只是動畫的第二個...

回答

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