2012-12-15 13 views
7

Chcę animować UISlider dla np. Od 0,25 do 0,75 iz powrotem. Powinno to pokazać użytkownikowi, co należy zrobić.Animacja UISlider płynnie

próbowałem to:

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

ale ów nie tak gładkie ... mogę używać przejścia do tego?

[Slider setValue:1 animated:YES]; 

jest szybko ...

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

Wystarczy ożywia drugi ...

Odpowiedz

14

Musisz łańcucha animacje umieszczając drugą animację w bloku zakończeniu pierwszego :

-(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 wersja dla rdelmar „S odpowiedzieć

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

i wywołać funkcję przejść UISlider w parametrze

animateSlider(self.slider)