2009-09-24 2 views
5

私は1.0fから0.0fにアニメーション化したい、1.0fに戻すAlphaプロパティを持つ単純なUIButtonを持っています。これは基本的に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

4

これは非常に簡単です:iPhoneのコードのポストのための

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

感謝。

2番目の答えはグローバル変数を使用しており、コールバックのパラメータをスキップします。 ここに最初の答えに基づいて私が今日考え出したものがあります。

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