按下按钮时如何切换标签的隐藏?

问题描述:

我想弄清楚如何只在操作系统中按下按钮时显示标签.我知道如何操作触摸事件,但我不确定如何将 UILongPressGestureRecognizer 合并到其中.

I am trying to figure out how to only display a label while a button is pressed in OS. I know how to operate the touch events but I am not sure how to incorporate the UILongPressGestureRecognizer into this.

UIButton 类以及许多其他 UIControl 子类可以将许多操作连接到

The UIButton class, as well as lots of other UIControl subclasses can have numerous actions hooked up to them.

当我们将一个操作从界面构建器连接到我们的源代码文件时,如果我们打开事件"下拉菜单,我们会看到一长串选项:

When we are hooking up an action from interface builder to our source code file, if we open the "Event" drop down, we're presented with a long list of options:

几乎在所有场景中,我们都只将我们的操作与Touch Up Inside"挂钩.这允许用户考虑他们是否真的想要按下按钮.如果他们在松开之前将手指从按钮上拖开,则不会触发该动作,因为向上触摸"手势发生在对象边界之外.

In almost every scenario, we're hooking our actions only up to "Touch Up Inside". This allows the user to consider whether or not they want to really press the button. If they drag their finger off the button before letting go, the action doesn't fire, because the "up touch" gesture happened outside the bounds of the object.

但在这里,我们想要实际挂钩按钮的触地"事件.这是我们将显示标签的时间.

But here, we want to actually hook our button's "touch down" event up. This is when we'll display the label.

让我们继续创建一个touch down"事件和一个touch up inside"事件:

Let's go ahead and create a "touch down" event and a "touch up inside" event:

@IBAction func buttonTouchDown(sender: UIButton) {
    self.myLabel.hidden = false
}

@IBAction func buttonTouchEnded(sender: UIButton) {
    self.myLabel.hidden = true
}

目标-C

- (IBAction)buttonTouchDown:(UIButton *)sender {
    self.myLabel.hidden = NO;
}

- (IBAction)buttonTouchEnded:(UIButton *)sender {
    self.myLabel.hidden = YES;
}

至此,buttonTouchEnded设置完全正常,buttonTouchDown是通过从Event"列表中选择touch down"设置的.

So far, buttonTouchEnded is set up completely normally, and buttonTouchDown was set up by selecting "touch down" from the "Event" list.

我们总是可以通过在界面构建器中右键单击来验证我们的控件连接到什么:

We can always verify what our control is hooked up to by right clicking it in the interface builder:

但是这个菜单不仅仅用于检查我们已经连接的内容.从这里,我们可以将任何其他动作连接到我们现有的 @IBAction 方法,只需点击圆圈并拖动到现有方法即可.

But this menu is useful for more than simply checking what we've already hooked up. From here, we can hook up any of the other actions to our existing @IBAction methods simply by clicking in the circle and dragging to the existing method.

因此,如果我们停止按下按钮,我们显然希望标签消失,就像您连接任何其他按钮一样正常修饰.剩下的唯一问题是,您想要什么确切的行为?

So we obviously want the label to disappear if we stop pressing the button, a normal touch up like you'd hook up any other button. The only question remaining is, what exact behavior do you want?

如果你想让标签只在手指抬起的时候消失,不管手指走到哪里,那么我们还要勾上touch up out".

If you want the label to disappear only when the finger is lifted, no matter where the finger goes, then we must also hook up "touch up outside".

如果您希望当用户将手指从按钮上拖出时标签消失,那么我们应该连接触摸拖动退出"操作.

If you want the label to disappear when the user drags their finger off the button, then we should hook up the "touch drag exit" action.

我们可能还想连接触摸取消"操作,如果某种系统事件(可能是来电)取消触摸,就会发生这种情况.

We also probably want to hook up the "touch cancel" action, which would occur if some sort of system event (perhaps an incoming phone call) cancels the touch.

此 Stack Overflow 答案 详细说明了我们拥有的操作选项之间的差异,因此您可以准确地设计行为你需要它.

This Stack Overflow answer elaborates on the differences between the action options we have, so you can craft the behavior exactly how you need it.

无论如何,一旦我们决定了哪些操作要与哪些方法挂钩,请调出右键菜单,然后从圆圈单击并拖动到方法:

Anyway, once we decide which actions we want to hook up to which methods, bring up that right click menu and click-drag from the circles to the methods: