CustomRenderer用于按钮背景颜色过渡

时间:2018-05-09 13:56:11

标签: xamarin xamarin.forms xamarin.ios

当用户点击我的按钮时,我已经创建了一个自定义程序来更改TitleColorBackgroundColor

出于某种原因,当按钮被聚焦时,TitleColor变得或多或少变得透明,而我的背景并不像我的TitleColor那样具有动画效果。

这是我的代码:

public class BtnRendereriOS : ButtonRenderer
{

    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        base.OnElementChanged(e);


        if (Control != null)
        {
            Control.Layer.CornerRadius = 0;

            BackgroundNormalState(Control);            

            Control.TouchUpInside += (object sender, EventArgs c) =>
            {
                BackgroundNormalState(sender);
            };
            Control.TouchUpOutside += (object sender, EventArgs c) =>
            {
                BackgroundNormalState(sender);
            };
            Control.TouchDragOutside += (object sender, EventArgs c) =>
            {
                BackgroundNormalState(sender);
            };
            Control.TouchDragInside += (object sender, EventArgs c) =>
            {
                BackgroundChangedState(sender);
            };

            Control.TouchDown += (object sender, EventArgs c) =>
            {
                BackgroundChangedState(sender);
            };
            void BackgroundNormalState(object sender)
            {
                Action a = () => Control.SetTitleColor(UIColor.FromRGB(255,255,255), UIControlState.Normal);
                UIView.PerformWithoutAnimation(a); 

                (sender as UIButton).BackgroundColor = UIColor.FromRGB(3, 169, 244);

            }
            void BackgroundChangedState(object sender)
            {
                Action a = () => Control.SetTitleColor(UIColor.FromRGB(0,0,0), UIControlState.Highlighted);
                UIView.PerformWithoutAnimation(a);

                (sender as UIButton).BackgroundColor = UIColor.FromRGB(104, 206, 253);
            }
        }
    } 
}

目前,我已从TitleColor删除动画。

我的问题是:

  • 如何向我的BackgroundColor添加与我TitleColor上的默认转换相同的转化?
  • 如何删除&#34;透明度&#34;当按钮被聚焦时从TitleColor开始?

2 个答案:

答案 0 :(得分:1)

  

如何删除&#34;透明度&#34;来自TitleColor的按钮   正在集中注意力?

这是系统按钮的功能,如果您不喜欢这样,请尝试使用自定义样式来避免它:

UIButton btn = new UIButton(UIButtonType.Custom);
btn.SetTitle(Control.TitleLabel.Text, UIControlState.Normal);
SetNativeControl(btn);
// Pay attention to that the title color just needs to be set once
btn.SetTitleColor(UIColor.FromRGB(255, 255, 255), UIControlState.Normal);
btn.SetTitleColor(UIColor.FromRGB(0, 0, 0), UIControlState.Highlighted);

然后,您可以使用UIView.Animate()更改按钮的背景颜色&#34;转换&#34;:

void BackgroundNormalState(object sender)
{
    UIView.Animate(0.2, () =>
    {
        (sender as UIButton).BackgroundColor = UIColor.FromRGB(3, 169, 244);
    });

}
void BackgroundChangedState(object sender)
{
    UIView.Animate(0.2, () =>
    {
        (sender as UIButton).BackgroundColor = UIColor.FromRGB(104, 206, 253);
    });
}

答案 1 :(得分:0)

您可以使用UIView.Animate|AsyncUIView.TransitionNotify|Async,具体取决于您要查找的最终结果。关键是将所有更改放在同一个动画块中,以便它们通过Core Animation(CA)同时出现。

以下是使用UIView.TransitionNotifyAsync在颜色变化之间使用TransitionCrossDissolve的示例。

// Set intial values
var normalTextColor = UIColor.FromRGB(255, 255, 255);
var normalBackgroundColor = UIColor.FromRGB(3, 169, 244);
var highlightTextColor = UIColor.Black;
var highlightBackgroundColor = UIColor.FromRGB(104, 206, 253);
Control.SetTitleColor(normalTextColor, UIControlState.Normal);
Control.BackgroundColor = normalBackgroundColor;

async Task NormalColorState(UIButton button)
{
    await UIView.TransitionNotifyAsync(button, .25, UIViewAnimationOptions.TransitionCrossDissolve, () =>
    {
        button.BackgroundColor = normalBackgroundColor;
        button.SetTitleColor(normalTextColor, UIControlState.Normal);
    });
}
Control.TouchDown += async (object sender, EventArgs e) =>
{
    var button = sender as UIButton;
    await UIView.TransitionNotifyAsync(button, .25, UIViewAnimationOptions.TransitionCrossDissolve, () =>
    {
        button.BackgroundColor = highlightBackgroundColor;
        button.SetTitleColor(highlightTextColor, UIControlState.Highlighted);
    });
};
Control.TouchUpInside += async (object sender, EventArgs e) =>
{
    await NormalColorState(sender as UIButton);
};
Control.TouchUpOutside += async (object sender, EventArgs e) =>
{
    await NormalColorState(sender as UIButton);
};