Как я могу анимировать свойство UIButton Alpha с помощью MonoTouch?

StackOverflow https://stackoverflow.com/questions/1470356

  •  16-09-2019
  •  | 
  •  

Вопрос

У меня есть простой UIButton со свойством Alpha, который я хотел бы анимировать от 1.0f до 0.0f, а затем обратно к 1.0f.По сути, это ответ на TouchDown.

Кроме того, нужно ли мне делать что-то особенное, если подпрограмма, из которой я вызываю, находится не в основном потоке (асинхронный делегат, вызываемый в ThreadPool)?

Должен ли я использовать CAAnimation?

Спасибо!

Это было полезно?

Решение

Если кто-то не предложит моно-способ сделать это, я говорю: используйте:

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

}

Другие советы

Спасибо за сообщение с кодом 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();
}

Это довольно просто:

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();
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top