【问题标题】:UILongPressGestureRecognizer in Xamarin IOSXamarin IOS 中的 UILongPressGestureRecognizer
【发布时间】:2021-09-14 13:45:37
【问题描述】:

我目前正在使用“按住”按钮,这是我的代码:

public override void AwakeFromNib()
{
    base.AwakeFromNib();

    longp = new UILongPressGestureRecognizer(LongPress);
    button.AddGestureRecognizer(longp);
}

public void LongPress()
{
    if (a == true)
    {
       a = false;
    }
    else
    {
       a = true;
    }
    // stop recognizing long press gesture here
}

问题是因为我正在运行一个切换方法来更改一个值,它所做的只是向 LongPress 方法发送垃圾邮件,我如何在更改值后取消或停止持有?

更新

我已经成功了,这是我的代码示例:

public void LongPress(UILongPressGestureRecognizer g)
{
    if (g.State == UIGestureRecognizerState.Began)
    {
        if (a == true)
        {
            a = false;
        }
        else
        {
            a = true;
        }
    }
}

【问题讨论】:

    标签: ios xamarin xamarin.ios uilongpressgesturerecogni


    【解决方案1】:

    尝试将UILongPressGestureRecognizer 作为参数传递给LongPress 方法,并在您想停止它时更改其State

    示例代码

    public void LongPress(UILongPressGestureRecognizer g)
    {
        if (a == true)
        {
            a = false;
        }
        else
        {
            a = true;
        }
    
        // stop recognizing long press gesture here
        g.State = UIGestureRecognizerState.Ended; 
    }
    

    【讨论】:

    • 谢谢你的建议,我已经成功了。我已经更新了我的代码示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多