2009-09-24 8 views
5

J'ai un simple UIButton avec une propriété Alpha que je voudrais animer de 1.0f à 0.0f, puis de nouveau à 1.0f. Ceci répond essentiellement à TouchDown.Comment puis-je animer une propriété UIButton Alpha avec MonoTouch?

De plus, y a-t-il quelque chose de spécial que je dois faire si la routine que j'appelle n'est pas sur le thread principal (délégué asynchrone invoqué sur le ThreadPool)?

Devrais-je utiliser CAAnimation?

Merci!

Répondre

6

À moins de tuyaux quelqu'un avec une manière mono de le faire, je dis utilisation:

- (void) pulseButton { 
    button.alpha = 0.0; 
    [UIView beginAnimations:nil context:button]; { 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDidStopSelector:@selector(makeVisibleAgain:finished:context:)]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
     [UIView setAnimationDuration:0.50]; 
     button.alpha = 0.0; 
    } [UIView commitAnimations]; 
} 
- (void)makeVisibleAgain:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    UIButton *button = ((UIButton *) context); 
    [UIView beginAnimations:nil context:nil]; { 
     [UIView setAnimationDelegate:nil]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
     [UIView setAnimationDuration:0.5]; 
     button.alpha = 1.0; 
    } [UIView commitAnimations]; 

} 
+0

Grande réponse; très facile à porter à mono – rpetrich

4

Ceci est assez simple:

UIView button; 

public void fadeButtonInAndOut() 
{ 
    UIView.BeginAnimations("fadeOut"); 
    UIView.SetAnimationDelegate(this); 
    UIView.SetAnimationDidStopSelector(new Selector("fadeOutDidFinish")); 
    UIView.SetAnimationDuration(0.5f); 
    button.Alpha = 0.0f; 
    UIView.CommitAnimations(); 
} 

[Export("fadeOutDidFinish")] 
public void FadeOutDidFinish() 
{ 
    UIView.BeginAnimations("fadeIn"); 
    UIView.SetAnimationDuration(0.5f); 
    button.Alpha = 1.0f; 
    UIView.CommitAnimations(); 
} 
5

Merci pour le code iPhone après.

La deuxième réponse utilise une variable globale et ignore les paramètres du rappel. Voici ce que j'ai compris aujourd'hui basé sur la première réponse.

private void BeginPulse (Button button) 
{ 
    UIView.BeginAnimations (button+"fadeIn", button.Handle); 
    UIView.SetAnimationDelegate (this); 
    UIView.SetAnimationDidStopSelector (new MonoTouch.ObjCRuntime.Selector ("makeVisibleAgain:finished:context:")); 
    UIView.SetAnimationCurve(UIViewAnimationCurve.EaseOut); 
    UIView.SetAnimationDuration (0.5); 
    button.Alpha = 0.25f; 
    UIView.CommitAnimations(); 
} 

[Export ("makeVisibleAgain:finished:context:")] 
private void EndPulse (NSString animationId, NSNumber finished, UIButton button) 
{ 
    UIView.BeginAnimations (null, System.IntPtr.Zero); 
    UIView.SetAnimationDelegate (this); 
    UIView.SetAnimationCurve (UIViewAnimationCurve.EaseIn); 
    UIView.SetAnimationDuration (0.5); 
    button.Alpha = 1; 
    UIView.CommitAnimations(); 
}