2009-09-24 6 views
5

Ich habe eine einfache UIButton mit einer Alpha-Eigenschaft, die ich von 1.0f zu 0.0f animieren möchte, und dann zurück zu 1.0f. Dies reagiert grundsätzlich auf TouchDown.Wie kann ich eine UIButton Alpha-Eigenschaft mit MonoTouch animieren

Gibt es auch etwas Spezielles, das ich tun muss, wenn die Routine, von der aus ich anrufe, nicht auf dem Hauptthread ist (Async-Delegat, das im ThreadPool aufgerufen wird)?

Sollte ich CAAnimation verwenden?

Danke!

Antwort

6

Es sei denn, jemand Rohre mit einem Mono-Weg, es zu tun, sage ich Gebrauch:

- (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

Große Antwort; sehr einfach zu mono portieren – rpetrich

4

Das ist ziemlich einfach:

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

Vielen Dank für die iPhone Code Post.

Die zweite Antwort verwendet eine globale Variable und überspringt die Parameter für den Rückruf. Hier ist, was ich heute anhand der ersten Antwort herausgefunden habe.

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