[英]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);
}
This code usually works very well! 这段代码通常效果很好!
However, it does not work in some of my classes for some unknown reasons... 但是,由于某些未知原因,它在我的某些课程中不起作用......
Here is the problem: 这是问题所在:
Things Checked: 事情检查:
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.