Come posso animare una proprietà UIButton Alpha con MonoTouch
-
16-09-2019 - |
Domanda
Ho una semplice UIButton con una proprietà Alpha che vorrei animare da 1.0f a 0.0f, e poi di nuovo a 1.0f. Questo è fondamentalmente rispondendo a touchdown.
Inoltre, c'è qualcosa di speciale che devo fare se la routine che chiamo da non è sul thread principale (asincrona delegato richiamato sul ThreadPool)?
Dovrei usare CAAnimation?
Grazie!
Soluzione
A meno che qualcuno tubi con un modo mono da fare, dico uso:
- (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];
}
Altri suggerimenti
Grazie per il codice postale iPhone.
La seconda risposta sta usando una variabile globale e ignora i parametri per la richiamata. Ecco quello che ho capito oggi sulla base della prima risposta.
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();
}
Questo è abbastanza semplice:
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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow