xcode 如何处理 UIView 中的点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9484193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:28:36  来源:igfitidea点击:

How to handle click event in a UIView

iosxcodeeventsclick

提问by haxpanel

I'm writing a simple card game and now I'd like to make the card clicking part of it.. All my cards are UIView objects and there is 2 UIImageView in it to show the back or the front of the cards. I want to handle the mouse click and I don't really know how to do it. Is there an interface to add? Thanks!

我正在编写一个简单的纸牌游戏,现在我想让卡片点击成为其中的一部分。我所有的卡片都是 UIView 对象,其中有 2 个 UIImageView 来显示卡片的背面或正面。我想处理鼠标点击,但我真的不知道该怎么做。有接口可以添加吗?谢谢!

回答by QED

You can make your cards buttons (perhaps not the best solution) and handle button events in your code. You could use gesture recognizers on your cards or your superview. You can make each card a subclass of UIView and override the UIRespondermethods which handle touch events.

您可以制作卡片按钮(可能不是最佳解决方案)并在代码中处理按钮事件。您可以在卡片或超级视图上使用手势识别器。您可以使每张卡片成为 UIView 的子类并覆盖处理触摸事件的UIResponder方法。

Or you can get really tricky and make the cards CALayers, and just handle UIResponder events in your parent view. This last suggestion will probably allow you the greatest flexibility in animating your cards around as the user plays your game.

或者你可以变得非常棘手并使卡片CALayers,并在你的父视图中处理 UIResponder 事件。最后一个建议可能会让您在用户玩游戏时为您的卡片设置动画时具有最大的灵活性。

Anyway I think the UIResponder methods are what you're after. UIView is a subclass of UIResponder, so you'll have to implement them in some subclass that you define.

无论如何,我认为 UIResponder 方法是您所追求的。UIView 是 UIResponder 的子类,因此您必须在您定义的某个子类中实现它们。

回答by Convolution

Have you looked into the UIGestureRecognizerclass? It's great for linking a function call to some kind of interaction. There are different type of gestures, taps, swipes, pans. It sounds like a tap gesture might help you out to change the UIImageView each time the card is tapped on.

你有没有研究过UIGestureRecognizer类?它非常适合将函数调用链接到某种交互。有不同类型的手势,点击,滑动,平移。听起来每次点击卡片时,点击手势可能会帮助您更改 UIImageView。

回答by EeKay

@Convolution is right: use UITapGestureRecognizer to recognize a tab on an object that has no click event. This is the easiest and cleanest solution.

@Convolution 是对的:使用 UITapGestureRecognizer 识别没有单击事件的对象上的选项卡。这是最简单、最干净的解决方案。

Subclassing in order to recognize a click only pays off when you are creating a custom object that you will be reusing a lot of the time.

为识别点击而进行的子类化只有在您创建一个将多次重用的自定义对象时才有意义。

回答by Michael Dautermann

Instead of using UIImageViewfor displaying the cards, consider using UIButtons(with "custom" for the button type so the only borders that are seen are the boundaries of the images). You can then set your card images to be the images for those buttons.

不是UIImageView用于显示卡片,而是考虑使用UIButtons(按钮类型为“自定义”,因此可见的唯一边界是图像的边界)。然后,您可以将卡片图像设置为这些按钮的图像。

回答by wizH

Simply set a custom button under the image. Then you just have to make the image non-touchable. This way the method you have hooked up with your button will get executed on touchevents.

只需在图像下设置一个自定义按钮。然后你只需要使图像不可触摸。这样,您与按钮关联的方法将在 touchevents 上执行。