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.
谢谢。
10 个解决方案
#1
137
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.
你可以在很多情况下使用这种技术你真的想要屏幕的某个区域作为一个按钮而不是弄乱触摸的东西。
#2
115
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.
UIImageView是从UIView派生出来的UIResponder派生出来的它可以处理触摸事件。您将希望提供touchesBegan、touchesMoved和touchesEnded方法,如果用户点击图像,它们将被调用。如果您想要的只是一个tap事件,那么只需使用一个自定义按钮,并将图像设置为按钮图像就更容易了。但如果你想要对水龙头、移动等进行更细粒度的控制,这是一条可行之路。
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).重写canBecomeFirstResponder并返回YES以指示视图可以成为touch事件的焦点(默认值是NO)。
-
Set the
userInteractionEnabled
property to YES. The default forUIViews
is YES, but forUIImageViews
is NO so you have to explicitly turn it on.将userInteractionEnabled属性设置为YES。uiview的默认值是YES,但uiimageview的默认值是NO所以你必须显式地打开它。
-
If you want to respond to multi-touch events (i.e. pinch, zoom, etc) you'll want to set
multipleTouchEnabled
to YES.如果您想要对多点触控事件(例如缩放、缩放等)做出响应,您需要将multipleTouchEnabled设置为YES。
#3
48
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.
希望有帮助。
#4
23
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];
#5
12
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.
在过去的几个小时里,我一直在尝试寻找解决问题的方法,但都无济于事。我看到很多devs都有这个问题,我想这里的人都知道。我在UIScrollView中有多个图像,试图获取它们上的tap事件。
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 .
- 为“UIImageView”和“父视图”设置的setUserInteractionEnabled为YES。
- set setMultipleTouchEnabled to YES for
UIImageView
. - 为UIImageView设置setMultipleTouchEnabled为YES。
- Tried subclassing
UIImageView
, didn't help any. - 尝试子类化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
在下面附加一些代码,在这段代码中我初始化了UIImageView和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.
如前所述,标签tap被接收。
#6
6
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然后把它放在navbar上,你应该创建一个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~
记住,这段代码进入了视图控制器它在导航控制器viewcontroller数组中。基本上,这个“可触摸的图像栏按钮项目”只会在显示视图控制器时出现在导航栏中。当你按下另一个视图控制器时,这个导航栏按钮项将消失~
#7
3
What you might want to do is override the touchesBegan:withEvent:
method of the UIView
(or subclass) that contains your UIImageView
subview.
您可能想要做的是重写包含您的UIImageView子视图(或子类)的touchesBegan:withEvent: method。
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来运行这个测试。
#8
0
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.
对于那些想要快速解决这个问题的人来说。您可以使用以下命令来检测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
}
#9
-1
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。
#10
-2
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
在那个视图上添加手势。将图像添加到视图中,那么它也将检测图像上的手势。你能试试触摸事件的委托方法吗在这种情况下,它也可能被检测到。谢谢
#1
137
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.
你可以在很多情况下使用这种技术你真的想要屏幕的某个区域作为一个按钮而不是弄乱触摸的东西。
#2
115
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.
UIImageView是从UIView派生出来的UIResponder派生出来的它可以处理触摸事件。您将希望提供touchesBegan、touchesMoved和touchesEnded方法,如果用户点击图像,它们将被调用。如果您想要的只是一个tap事件,那么只需使用一个自定义按钮,并将图像设置为按钮图像就更容易了。但如果你想要对水龙头、移动等进行更细粒度的控制,这是一条可行之路。
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).重写canBecomeFirstResponder并返回YES以指示视图可以成为touch事件的焦点(默认值是NO)。
-
Set the
userInteractionEnabled
property to YES. The default forUIViews
is YES, but forUIImageViews
is NO so you have to explicitly turn it on.将userInteractionEnabled属性设置为YES。uiview的默认值是YES,但uiimageview的默认值是NO所以你必须显式地打开它。
-
If you want to respond to multi-touch events (i.e. pinch, zoom, etc) you'll want to set
multipleTouchEnabled
to YES.如果您想要对多点触控事件(例如缩放、缩放等)做出响应,您需要将multipleTouchEnabled设置为YES。
#3
48
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.
希望有帮助。
#4
23
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];
#5
12
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.
在过去的几个小时里,我一直在尝试寻找解决问题的方法,但都无济于事。我看到很多devs都有这个问题,我想这里的人都知道。我在UIScrollView中有多个图像,试图获取它们上的tap事件。
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 .
- 为“UIImageView”和“父视图”设置的setUserInteractionEnabled为YES。
- set setMultipleTouchEnabled to YES for
UIImageView
. - 为UIImageView设置setMultipleTouchEnabled为YES。
- Tried subclassing
UIImageView
, didn't help any. - 尝试子类化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
在下面附加一些代码,在这段代码中我初始化了UIImageView和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.
如前所述,标签tap被接收。
#6
6
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然后把它放在navbar上,你应该创建一个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~
记住,这段代码进入了视图控制器它在导航控制器viewcontroller数组中。基本上,这个“可触摸的图像栏按钮项目”只会在显示视图控制器时出现在导航栏中。当你按下另一个视图控制器时,这个导航栏按钮项将消失~
#7
3
What you might want to do is override the touchesBegan:withEvent:
method of the UIView
(or subclass) that contains your UIImageView
subview.
您可能想要做的是重写包含您的UIImageView子视图(或子类)的touchesBegan:withEvent: method。
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来运行这个测试。
#8
0
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.
对于那些想要快速解决这个问题的人来说。您可以使用以下命令来检测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
}
#9
-1
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。
#10
-2
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
在那个视图上添加手势。将图像添加到视图中,那么它也将检测图像上的手势。你能试试触摸事件的委托方法吗在这种情况下,它也可能被检测到。谢谢