2012-12-15 21 views
7

voglio animare un UISlider per es. Da 0,25 a 0,75 e ritorno. Questo dovrebbe mostrare all'utente cosa deve fare.Animate UISlider uniformemente

ho provato questo:

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

ma quello non è così liscio ... posso utilizzare le transizioni per questo?

[Slider setValue:1 animated:YES]; 

è di digiunare ...

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

anima la Proprio il secondo ...

risposta

14

È necessario catena le animazioni mettendo il secondo di animazione nel blocco completamento del primo :

-(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 versione per rdelmar 'S rispondere

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

e di chiamare la funzione passare l'UISlider nel parametro

animateSlider(self.slider)