当用户点击我的按钮时,我已经创建了一个自定义程序来更改TitleColor
和BackgroundColor
。
出于某种原因,当按钮被聚焦时,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
上的默认转换相同的转化?TitleColor
开始?答案 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|Async
或UIView.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);
};