ios UIView 上的触摸事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4130496/
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
Touch Event on UIView
提问by Andrew M
Does anyone have any sample code for detecting touches on a dynamically created UIView? I have found some references to touchesBegan but cannot figure out how to implement it...
有没有人有任何示例代码来检测动态创建的 UIView 上的触摸?我发现了一些对 touchesBegan 的引用,但无法弄清楚如何实现它......
回答by Tyler
A very general way to get touches is to override these methods in a custom UIView subclass:
一个非常通用的方法是在自定义 UIView 子类中覆盖这些方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
The official docs for these methods is under Responding to Touch Eventsin the UIResponder class docs(UIView inherits those methods since it's a subclass of UIResponder). Longer and more introductory docs can be found in the Event Handling Guide for iOS.
这些方法的官方文档位于UIResponder 类文档中的“响应触摸事件”下(UIView 继承了这些方法,因为它是 UIResponder 的子类)。更长和更多的介绍性文档可以在iOS的事件处理指南中找到。
If you just want to detect a tap (touch-down, then touch-up within your view), it's easiest to add a UIButton
as a subview of your view, and add your own custom method as a target/action pair for that button. Custom buttons are invisible by default, so it wouldn't affect the look of your view.
如果您只想检测点击(在您的视图中触摸,然后触摸),最简单的方法是将 a 添加UIButton
为您的视图的子视图,并将您自己的自定义方法添加为该按钮的目标/操作对。默认情况下,自定义按钮是不可见的,因此它不会影响视图的外观。
If you're looking for more advanced interactions, it's also good to know about the UIGestureRecognizer
class.
如果你正在寻找更先进的互动,这也是很好的了解了UIGestureRecognizer
类。
回答by ArunHyman
Create touch event with UIAnimation on UIView , you can manage touches on UIView anywhere .
在 UIView 上使用 UIAnimation 创建触摸事件,您可以在任何地方管理 UIView 上的触摸。
Following Code : here self.finalScore is a UIView , and cup is a UIImageView . I handle
以下代码:这里 self.finalScore 是一个 UIView ,cup 是一个 UIImageView 。我处理
the touch event on UIImageView , it present inside the UIView .
UIImageView 上的触摸事件,它出现在 UIView 中。
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [touches anyObject];
CGPoint touchLocation = [touch1 locationInView:self.finalScore];
CGRect startRect = [[[cup layer] presentationLayer] frame];
CGRectContainsPoint(startRect, touchLocation);
[UIView animateWithDuration:0.7
delay:0.0
options:UIViewAnimationCurveEaseOut
animations:^{cup.transform = CGAffineTransformMakeScale(1.25, 0.75);}
completion:^(BOOL finished) {
[UIView animateWithDuration:2.0
delay:2.0
options:0
animations:^{cup.alpha = 0.0;}
completion:^(BOOL finished) {
[cup removeFromSuperview];
cup = nil;}];
}];
}
Like UITapGestureRecognizer is another way of handle the touch events on UIView ....
像 UITapGestureRecognizer 是处理 UIView 上触摸事件的另一种方式......
UITapGestureRecognizer *touchOnView = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(releaseButAction)] autorelease];
// Set required taps and number of touches
[touchOnView setNumberOfTapsRequired:1];
[touchOnView setNumberOfTouchesRequired:1];
// Add the gesture to the view
[[self view] addGestureRecognizer:touchOnView];
回答by ThE uSeFuL
Create your own custom UIView
from a subclass you created by inheriting UIView
and override the following method inside the sub-class,
UIView
通过继承UIView
和覆盖子类中的以下方法,从您创建的子类创建您自己的自定义,
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
回答by David.Chu.ca
With gesture recognizer, it is much easy to catch touch events.
使用手势识别器,很容易捕捉触摸事件。
Find one guested from component library in storyboard:
在故事板的组件库中找到一个guested:
For example, drag Tap Gesture Recognizer to your view. It is not shown in the view UI. You can find it from Document outline in the left panel in Storyboard:
例如,将 Tap Gesture Recognizer 拖到您的视图中。它不会显示在视图 UI 中。您可以从 Storyboard 左侧面板的 Document outline 中找到它:
The final step is to link it (by control-drag) to the view as delegate where touch is monitored, and control-drag to your view controller class as an action. Here is the picture of link connections.
最后一步是将它(通过控制拖动)链接到作为监视触摸的委托的视图,并控制拖动到您的视图控制器类作为一个动作。这是链接连接的图片。
and action code:
和操作代码:
@IBAction func tapAction(_ sender: Any) {
// touch is captured here.
}
回答by Zoeb S
Instead of capturing touch using gesture, I would suggest to change UIView parent class to UIControl, so that it can handle touchUpInside event
我建议将 UIView 父类更改为 UIControl,而不是使用手势捕获触摸,以便它可以处理 touchUpInside 事件
From:
从:
@interface MyView : UIView
To:
到:
@interface MyView : UIControl
and then add this line for view-
然后添加此行以供查看-
[self addTarget:self action:@selector(viewTapped:) forControlEvents:UIControlEventTouchUpInside];
Because gesture may create problem while scrolling.
因为手势可能会在滚动时产生问题。
回答by DazChong
My method is simply change the UIView
to UIControl
class in InterfaceBuilder IdentityInspector, then the ConnectionsInspector will instantly have the touch event ready for hook.
我的方法是简单地更改InterfaceBuilder IdentityInspector 中的UIView
toUIControl
类,然后 ConnectionsInspector 将立即为挂钩准备触摸事件。
zero code changes.
零代码更改。