繁体   English   中英

圆形自定义 UIButton 不显示触摸效果

[英]Rounded Custom UIButton does not exhibit the touch effect

我正在创建一个自定义 UIButton,如下所示:

public class RoundButton : UIButton
{
    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        Layer.CornerRadius = Bounds.Height / 2;
    }

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        this.Highlighted = true;
        base.TouchesBegan(touches, evt);
    }

    public override void TouchesEnded(NSSet touches, UIEvent evt)
    {
        this.Highlighted = false;
        base.TouchesEnded(touches, evt);
    }
    public override void TouchesCancelled(NSSet touches, UIEvent evt)
    {
        this.Highlighted = false;
        base.TouchesCancelled(touches, evt);
    }
}

我是这样使用的:

    var submitButton = new RoundButton();

    submitButton.Frame = new CGRect(10, 170, w - 20, 44);

    submitButton.SetTitle(Utilities.GetLocalizedString("connection"), UIControlState.Normal);
    submitButton.SetTitleColor(UIColor.White, UIControlState.Normal);
    submitButton.BackgroundColor = new UIColor(red: 1.00f, green: 0.37f, blue: 0.00f, alpha: 1.0f);
    submitButton.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;

    EventHandler p = async (sender, e) =>
    {
        Logging.Debug(@"Submit button pressed");
        await Application.m_webAccess.LoginAsync(usernameField.Text, passwordField.Text, Application.MyCancellationTokenSource.Token).ContinueWith(t => LoginResult(t.Result)).ConfigureAwait(continueOnCapturedContext: true);

    };

    submitButton.TouchUpInside -= p;
    submitButton.TouchUpInside += p;

尽管调用了 Touches* 覆盖,但在视觉上看不到触摸效果。

这里有什么问题?

您可以在按钮触摸事件上添加动画以根据需要实现效果。 例如,设置 BackgroundColor 渐变。

public class RoundButton : UIButton
{
    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        Layer.CornerRadius = Bounds.Height / 2;
    }

    public override void TouchesBegan(NSSet touches, UIEvent evt)
    {
        this.Highlighted = true;

        SetAnimation(UIColor.LightGray);

        base.TouchesBegan(touches, evt);
    }

    public override void TouchesEnded(NSSet touches, UIEvent evt)
    {
        this.Highlighted = false;

        SetAnimation(UIColor.Orange);

        base.TouchesEnded(touches, evt);
    }
    public override void TouchesCancelled(NSSet touches, UIEvent evt)
    {
        this.Highlighted = false;
        base.TouchesCancelled(touches, evt);
    }


    void SetAnimation(UIColor color)
    {
        UIView.BeginAnimations(null);
        UIView.SetAnimationDuration(1.0);
        UIView.SetAnimationTransition(UIViewAnimationTransition.None, this, false);

        this.Layer.BackgroundColor = color.CGColor;

        UIView.CommitAnimations();
    }

}

有关 iOS 中动画的更多详细信息,您可以查看文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM