简体   繁体   English

Monotouch TouchUpInside不起作用,TouchDown可以使用

[英]Monotouch TouchUpInside does not work, TouchDown works

I have seen some topics about this subject in Objective-C. 我在Objective-C中看到了关于这个主题的一些主题。 I read a lot of them, spent 2 days on it on trying to find a solution and none of them worked for me. 我读了很多,花了两天时间试图找到解决方案,但没有一个能为我工作。 I am mainly coding in C#. 我主要用C#编码。 Since my problem behaviour (fire only when leaving/re-enter button) and context (C#) is a bit different. 由于我的问题行为(仅在离开/重新输入按钮时触发)和上下文(C#)有点不同。 So, I will try my chance by asking my question here. 所以,我会在这里问我的问题,试试我的机会。

I will try to keep it simple. 我会尽量保持简单。

Here is a sample of code: 这是一个代码示例:

private UIButton _buttonTest;

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

    _buttonTest = new UIButton(new RectangleF(10, 70, 50, 50));
    _buttonTest.SetTitle("0", UIControlState.Normal);
    _buttonTest.TouchUpInside += HandleButtonTestTouchUpInside;
    _buttonTest.BackgroundColor = UIColor.Red;
    this.View.AddSubview(_buttonTest);
}

void HandleButtonTestTouchUpInside (object sender, EventArgs e)
{
    string textNumber = _buttonTest.Title(UIControlState.Normal);

    // Increment Number
    _buttonTest.SetTitle((int.Parse(textNumber)+1).ToString(), UIControlState.Normal);
}
  • There is a button and the title text is set to "0". 有一个按钮,标题文本设置为“0”。
  • When the user click on it, it increments the number (title) by 1. 当用户单击它时,它会将数字(标题)增加1。

This code usually works very well! 这段代码通常效果很好!

However, it does not work in some of my classes for some unknown reasons... 但是,由于某些未知原因,它在我的某些课程中不起作用......

Here is the problem: 这是问题所在:

  • TouchUpInside does not fire. TouchUpInside不会触发。 ... However, if I hold the button with a finger and keep holding the finger while leaving the button and then re-entering the button then release the button, then.... it will fire the TouchUpInside, .... so the finger need to leave and re-renter the button while holding the button to make the TouchUpInside fires. ...但是,如果我用手指按住按钮并在离开按钮的同时保持手指然后重新进入按钮然后释放按钮,那么......它将触发TouchUpInside,....所以手指需要离开并重新租用按钮,同时按住按钮以使TouchUpInside触发。 Usually, this code works very well. 通常,此代码非常有效。

Things Checked: 事情检查:

  • if I replace TouchUpInside by TouchDown, then TouchDown works. 如果我用TouchDown替换TouchUpInside,那么TouchDown会起作用。
  • 'User Interaction Enabled' is set to True for all superviews. 对于所有超级视图,“User Interaction Enabled”设置为True。
  • All views frames (among the superviews) seem to be inside their superview frames. 所有视图框架(在超视图中)似乎都在其超视图框架内。
  • I moved the _buttonTest around and I noticed that the _buttonTest TouchUpInside does not fire correctly in the View, the SuperView, the SuperSuperView.... but it fires correctly in the SuperSuperSuperView. 我移动了_buttonTest,我注意到_buttonTest TouchUpInside在View,SuperView,SuperSuperView中没有正确触发....但它在SuperSuperSuperView中正确触发。

Any suggestion? 有什么建议吗?

In the SuperSuperView, I had this tap gesture that was entering in conflict with the Button event. 在SuperSuperView中,我有一个与Button事件冲突的轻击手势。

    // Tap Gesture
    UITapGestureRecognizer tapPageGestureRecognizer = new UITapGestureRecognizer();
    tapPageGestureRecognizer.AddTarget(this, new Selector ("HandleTapPageGestureRecognizer:"));         
    this.View.AddGestureRecognizer(tapPageGestureRecognizer);

The idea is to disable the gesture SuperSuperView gesture when the button event, TouchDown was fired, ... and to re-enable it when the TouchUpInside is fired. 想法是在按钮事件,触发TouchDown时禁用手势SuperSuperView手势,并在触发TouchUpInside时重新启用它。

So here is one solution for the problem: 所以这是问题的一个解决方案:

private void SetGrandParentViewGestureEnabled(bool enabled)
{
    foreach(UIGestureRecognizer g in this.View.Superview.Superview.GestureRecognizers)
    {
        g.Enabled = enabled;
    }
}

void HandleButtonSubmitTouchDown (object sender, EventArgs e)
{
    SetGrandParentViewGestureEnabled(false);
}

void HandleButtonSubmitTouchUpInside (object sender, EventArgs e)
{
    // => Do treatments here!

    SetGrandParentViewGestureEnabled(true);
}

However, I could have used EventHandler or Action to enable/disable the tap gesture. 但是,我可以使用EventHandler或Action来启用/禁用轻击手势。

EDIT: Here is another function that need to be added as well to re-enable the gesture. 编辑:这是另一个需要添加的功能,以重新启用手势。

void HandleButtonSubmitTouchUpOutside (object sender, EventArgs e)
{
    SetGrandParentViewGestureEnabled(true);
}

I had a similar problem, I ended up using a tap recognizer together with the long press recognizer like this for the button TopRightButton. 我有一个类似的问题,我最终使用了一个水龙头识别器和这样的长按识别器,用于按钮TopRightButton。

     var longPressGesture = new UILongPressGestureRecognizer(Action);
            TopRightButton.AddGestureRecognizer(longPressGesture);

            var tapRecognizer = new UITapGestureRecognizer();

            tapRecognizer.AddTarget(() =>
                                    {
            //method
            });

            tapRecognizer.NumberOfTapsRequired = 1;

            this.TopRightButton.AddGestureRecognizer(tapRecognizer); 
private void Action(){  };

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

相关问题 如何检索按钮上的TouchUpInside返回(MonoTouch)? - How do I retrieve the TouchUpInside return on a button (MonoTouch)? 组合框TouchDown和MouseDown不会触发 - Combobox TouchDown and MouseDown does not fire 如何在 Monotouch Button TouchDown 事件处理程序中的 while 循环中休眠执行 - How to sleep execution in while cycle in Monotouch Button TouchDown event handler MonoTouch-在按钮touchUpInside委托内部未调用UIView.Animate完成回调 - MonoTouch - UIView.Animate completion callback is not called inside button touchUpInside delegate 在设备上运行时,MonoTouch 4.2不支持System.ServiceModel.EndpointAddress()(在模拟器上运行) - MonoTouch 4.2 does not support System.ServiceModel.EndpointAddress() when running on the device (works on the simulator) 在设备上运行时,MonoTouch不支持OpenAsync方法打开Web服务连接(在模拟器上工作) - MonoTouch does not support OpenAsync method for opening a web service connection when running on the device (works on the simulator) 如何在MonoTouch中使UIDocumentInteractionController工作 - How to make work UIDocumentInteractionController in MonoTouch ProgressHUD和TouchUpInside - ProgressHUD and TouchUpInside MonoTouch还支持NSInvocationOperation吗? - Does MonoTouch support NSInvocationOperation yet? 此CGPDFDictionaryGetString如何转换为Monotouch? - How does this CGPDFDictionaryGetString translate into Monotouch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM