【问题标题】:How can I animate a UIButton Alpha property with MonoTouch如何使用 MonoTouch 为 UIButton Alpha 属性设置动画
【发布时间】:2009-09-24 08:11:08
【问题描述】:

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

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

我应该使用 CAAnimation 吗?

谢谢!

【问题讨论】:

    标签: iphone uikit xamarin.ios


    【解决方案1】:

    除非有人用单声道方法来做,否则我说使用:

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

    【讨论】:

    • 很好的答案;很容易移植到单声道
    【解决方案2】:

    感谢您发布 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();
    }
    

    【讨论】:

      【解决方案3】:

      这很简单:

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

      【讨论】:

        猜你喜欢
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-04
        • 1970-01-01
        相关资源
        最近更新 更多