如何使用MonoTouch为UIButton Alpha属性设置动画

ben*_*ine 5 iphone uikit xamarin.ios

我有一个带有Alpha属性的简单UIButton,我想从1.0f动画到0.0f,然后再回到1.0f.这基本上是对TouchDown的响应.

另外,如果我调用的例程不在主线程上(在ThreadPool上调用异步委托),我需要做些什么特别的事情吗?

我应该使用CAAnimation吗?

谢谢!

mah*_*udz 6

除非有人用单声道方式进行操作,否则我说使用:

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

}
Run Code Online (Sandbox Code Playgroud)


ben*_*ine 5

感谢iPhone代码帖子.

第二个答案是使用全局变量并跳过回调的参数.这是我今天根据第一个答案想出来的.

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();
}
Run Code Online (Sandbox Code Playgroud)