2009-09-24 63 views
5

我有一个简单的带有Alpha属性的UIButton,我想从1.0f动画到0.0f,然后回到1.0f。这基本上是对TouchDown的回应。我如何使用MonoTouch为UIButton Alpha属性设置动画

此外,有什么特别的我需要做的,如果我打来的例程不在主线程(在ThreadPool上调用的异步委托)?

我应该使用CAAnimation吗?

谢谢!

回答

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

} 
+0

很好的回答;非常容易移植到单声道 – rpetrich 2009-09-25 04:09:38

4

这是非常简单的:

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

谢谢你的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(); 
} 
相关问题