ios 我如何检测 UIImageView 的触摸事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/855095/
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
how can i detect the touch event of an UIImageView
提问by ebaccount
I have placed an image (UIImageView) on the navigation bar. Now I want to detect the touch event and want to handle the event. Could you let me know how can I do that?
我在导航栏上放置了一个图像(UIImageView)。现在我想检测触摸事件并想要处理该事件。你能告诉我我该怎么做吗?
Thanks.
谢谢。
回答by Kendall Helmstetter Gelner
In practical terms, don't do that.
实际上,不要那样做。
Instead add a button with Custom style (no button graphics unless you specify images) over the UIImageView. Then attach whatever methods you want called to that.
而是在 UIImageView 上添加一个具有自定义样式的按钮(除非您指定图像,否则没有按钮图形)。然后附加任何你想调用的方法。
You can use that technique for many cases where you really want some area of the screen to act as a button instead of messing with the Touch stuff.
您可以在许多情况下使用该技术,在这种情况下,您确实希望屏幕的某些区域充当按钮而不是弄乱 Touch 的东西。
回答by Ramin
A UIImageView
is derived from a UIView
which is derived from UIResponder
so it's ready to handle touch events. You'll want to provide the touchesBegan
, touchesMoved
, and touchesEnded
methods and they'll get called if the user taps the image. If all you want is a tap event, it's easier to just use a custom button with the image set as the button image. But if you want finer-grain control over taps, moves, etc. this is the way to go.
AUIImageView
派生自 aUIView
派生自,UIResponder
因此它已准备好处理触摸事件。您将要提供的touchesBegan
,touchesMoved
和touchesEnded
方法,如果用户点击图片就可以调用。如果您想要的只是点击事件,那么使用自定义按钮并将图像设置为按钮图像会更容易。但是如果你想对点击、移动等进行更精细的控制,这就是要走的路。
You'll also want to look at a few more things:
您还需要查看更多内容:
Override
canBecomeFirstResponder
and return YES to indicate that the view can become the focus of touch events (the default is NO).Set the
userInteractionEnabled
property to YES. The default forUIViews
is YES, but forUIImageViews
is NO so you have to explicitly turn it on.If you want to respond to multi-touch events (i.e. pinch, zoom, etc) you'll want to set
multipleTouchEnabled
to YES.
覆盖
canBecomeFirstResponder
并返回 YES 表示该视图可以成为触摸事件的焦点(默认为 NO)。将该
userInteractionEnabled
属性设置为 YES。默认为UIViews
YES,但 forUIImageViews
为 NO,因此您必须明确打开它。如果您想响应多点触控事件(即捏合、缩放等),您需要设置
multipleTouchEnabled
为 YES。
回答by Yup.
To add a touch event to a UIImageView use the following in your .m
要将触摸事件添加到 UIImageView,请在 .m 中使用以下内容
UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTapMethod)];
[myImageView setUserInteractionEnabled:YES];
[myImageView addGestureRecognizer:newTap];
-(void)myTapMethod{
// treat image tap
}
hope that helps.
希望有帮助。
回答by spolu
You can also add a UIGestureRecognizer. It does not require you to add an additional element in your view hierarchy, but still provides you will all the nicely written code for handling touch events with a fairly simple interface:
您还可以添加 UIGestureRecognizer。它不需要您在视图层次结构中添加额外的元素,但仍然为您提供所有编写精美的代码,用于使用相当简单的界面处理触摸事件:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipe:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[imgView_ addGestureRecognizer:swipeRight];
[swipeRight release];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[imgView_ addGestureRecognizer:swipeLeft];
[swipeLeft release];
回答by ilan
I've been on different threads on the past few hours trying to find a solution for my problem, to no avail.
I see that many devs share this problem, and I think people here know about this.
I have multiple images inside a UIScrollView
, trying to get tap events on them.
在过去的几个小时里,我一直在不同的线程上试图为我的问题找到解决方案,但无济于事。我看到许多开发人员都有这个问题,我认为这里的人都知道这一点。我在 a 中有多个图像UIScrollView
,试图在它们上获取点击事件。
not getting any events from an UIImangeView
, but I do get an event from a similar UILable
with very similar parameters I am setting to it. under IOS 5.1.
没有得到来自任何事件UIImangeView
,但我得到了类似的事件UILable
非常相似的参数我设置它。在 IOS 5.1 下。
I have already done the following:
我已经做了以下事情:
- set setUserInteractionEnabled to YES for both `UIImageView and parent view .
- set setMultipleTouchEnabled to YES for
UIImageView
. - Tried subclassing
UIImageView
, didn't help any.
- 将`UIImageView 和父视图的setUserInteractionEnabled 设置为YES。
- 将 setMultipleTouchEnabled 设置为 YES
UIImageView
。 - 尝试子类化
UIImageView
,没有任何帮助。
Attaching some code below, in this code I initialize both a UIImageView
and UILabel
, the label works fine in terms of firing events. I tried keeping out irrelevant code.
Thanks guys,
Ilan
下面附上一些代码,在这段代码中,我同时初始化了 aUIImageView
和UILabel
,标签在触发事件方面工作正常。我尝试保留不相关的代码。谢谢你们,伊兰
UIImageView *single_view = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
single_view.image=img;
single_view.layer.zPosition=4;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[single_view addGestureRecognizer:singleTap];
[single_view setMultipleTouchEnabled:YES];
[single_view setUserInteractionEnabled:YES];
[self.myScrollView addSubview:single_view];
self.myScrollView.userInteractionEnabled=YES;
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
testLabel.backgroundColor=[UIColor redColor];
[self.myScrollView addSubview:testLabel];
[testLabel addGestureRecognizer:singleTap];
[testLabel setMultipleTouchEnabled:YES];
[testLabel setUserInteractionEnabled:YES];
testLabel.layer.zPosition=4;
And the method which handles the event:
以及处理事件的方法:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
UIView *tappedView = [gesture.view hitTest:[gesture locationInView:gesture.view] withEvent:nil];
NSLog(@"Touch event on view: %@",[tappedView class]);
}
As said, the label tap is received.
如上所述,接收到标签抽头。
回答by yujean
Instead of making a touchable UIImageView then placing it on the navbar, you should just create a UIBarButtonItem, which you make out of a UIImageView.
与其制作可触摸的 UIImageView,然后将其放置在导航栏上,不如创建一个 UIBarButtonItem,它由 UIImageView 制作。
First make the image view:
首先制作图像视图:
UIImageView *yourImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nameOfYourImage.png"]];
Then make the barbutton item out of your image view:
然后从图像视图中制作 barbutton 项目:
UIBarButtonItem *yourBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourImageView];
Then add the bar button item to your navigation bar:
然后将栏按钮项添加到导航栏:
self.navigationItem.rightBarButtonItem = yourBarButtonItem;
Remember that this code goes into the view controller which is inside a navigation controller viewcontroller array. So basically, this "touchable image-looking bar button item" will only appear in the navigation bar when this view controller when it's being shown. When you push another view controller, this navigation bar button item will disappear~
请记住,此代码进入导航控制器视图控制器数组内的视图控制器。所以基本上,这个“可触摸的图像栏按钮项”只会在显示此视图控制器时出现在导航栏中。当你再推一个view controller时,这个导航栏按钮项就会消失~
回答by Alex Reynolds
What you might want to do is override the touchesBegan:withEvent:
method of the UIView
(or subclass) that contains your UIImageView
subview.
您可能想要做的是覆盖包含您的子视图touchesBegan:withEvent:
的UIView
(或子类)的方法UIImageView
。
Within this method, test if any of the UITouch
touches fall inside the bounds of the UIImageView
instance (let's say it is called imageView
).
在这个方法中,测试是否有任何UITouch
触摸落在UIImageView
实例的边界内(假设它被称为imageView
)。
That is, does the CGPoint
element [touch locationInView]
intersect with with the CGRect
element [imageView bounds]
? Look into the function CGRectContainsPoint
to run this test.
也就是说,它的CGPoint
元素[touch locationInView]
相交用的CGRect
元素[imageView bounds]
?查看CGRectContainsPoint
运行此测试的函数。
回答by Hilaj
First you should place an UIButton then either you vcan add background image for this button or you need to place an UIImageView over the button.
首先你应该放置一个 UIButton 然后你可以为这个按钮添加背景图像或者你需要在按钮上放置一个 UIImageView 。
or
或者
You can add the tap gesture to a UIImageView so that get the click action when tap on the UIImageView.
您可以将点击手势添加到 UIImageView,以便在点击 UIImageView 时获得点击动作。
回答by Josh
For those of you looking for a Swift 4 solution to this answer. You can use the following to detect a touch event on a UIImageView.
对于那些正在寻找针对此答案的 Swift 4 解决方案的人。您可以使用以下方法检测 UIImageView 上的触摸事件。
let gestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
imageView.addGestureRecognizer(gestureRecognizer)
imageView.isUserInteractionEnabled = true
You will then need to define your selector as follows:
然后,您需要按如下方式定义选择器:
@objc func imageViewTapped() {
// Image has been tapped
}
回答by user1240409
Add gesture on that view.add image into that view then it would be detecting gesture on image too.Could you try with the delegate method of touch event then in that case also it might be detecting. Thanks
在该视图上添加手势。将图像添加到该视图中,然后它也会检测图像上的手势。您可以尝试使用触摸事件的委托方法,然后在这种情况下它也可能会检测到。谢谢