views:

936

answers:

3

I have a simple UIButton with an Alpha property that I would like to animate from 1.0f to 0.0f, and then back to 1.0f. This is basically responding to TouchDown.

Also, is there anything special I need to do if the routine I am calling from is not on the main thread (async delegate invoked on the ThreadPool)?

Should I use CAAnimation?

Thanks!

+5  A: 

Unless someone pipes up with a mono way to do it, I say use:

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

}
mahboudz
Great answer; very easy to port to mono
rpetrich
+3  A: 

This is pretty 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();
}
rpetrich
+4  A: 

Thanks for the iPhone code post.

The second answer is using a global variable and skips the parameters for the callback. Here is what I figured out today based on the first answer.

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