xcode 为 UIImageView 实现 UITouchDown
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4930449/
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
Implement UITouchDown for UIImageView
提问by Anurag
I know how to implement touchesBegan
for UIImageView
我知道如何实现touchesBegan
的UIImageView
is it possible to implement UITouchDown
for UIImageView
?(i know that i can use touchesBegan
instead of UITouchDown
but i want to implement UITouchDown
)
是有可能实现UITouchDown
的UIImageView
?(我知道我可以使用touchesBegan
,而不是UITouchDown
,但我想实现UITouchDown
)
回答by Anurag
Use a UIButton.
使用 UIButton。
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:someUIImage forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
Alternative to UIButton (longer method)
UIButton 的替代品(更长的方法)
A UIControl
implements user interactions and support for fine-grained user interactions already. You can combine the functionality of a UIImageView
and a UIControl
to achieve this since both are subclasses of UIView
.
A 已经UIControl
实现了用户交互并支持细粒度的用户交互。您可以结合 aUIImageView
和 a的功能UIControl
来实现这一点,因为它们都是UIView
.
To get this behavior, add an object of UIImageView as a subview to a UIControl
such that the image is fully covered. Then add a event handler to this control using addTarget:action:forControlEvents:
. Here's an example:
要获得此行为,请将 UIImageView 的对象作为子视图添加到 a UIControl
,以便完全覆盖图像。然后使用 向该控件添加一个事件处理程序addTarget:action:forControlEvents:
。下面是一个例子:
// assuming the image view object exists
UIImageView *anImageView = ..;
// create a mask that the covers the image exactly
UIControl *mask = [[UIControl alloc] initWithFrame:anImageView.frame];
// add the image as a subview of this mask
CGSize imageSize = anImageView.frame.size;
anImageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
[mask addSubview:anImageView];
// add a target-action for the desired control events to this mask
[mask addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
// add the mask as a subview instead of the image
[self.view addSubview:mask];
回答by Yariv Nissim
You can use UITapGestureRecognizer
, instead of creating custom UIButton
.
您可以使用UITapGestureRecognizer
, 而不是创建自定义UIButton
.
回答by Swapnil Luktuke
You can't.
你不能。
control events such as UIControlEventTouchDown
etc are available for UIControl's
child objects like UIButton
, UITextField
etc. UIImageView
is a UIView
and hence cannot generate control notifications.
控制事件诸如UIControlEventTouchDown
为等是可用UIControl's
的子对象等UIButton
,UITextField
等UIImageView
是一个UIView
,因此不能产生控制通知。
You have to use touchesBegan
and co.
你必须使用touchesBegan
和合作。
回答by Swapnil Luktuke
control events such as UIControlEventTouchDown etc are available for UIControl's child objects like UIButton, UITextField etc. UIImageView is a UIView and hence cannot generate control notifications.
UIControlEventTouchDown 等控件事件可用于 UIControl 的子对象,如 UIButton、UITextField 等。 UIImageView 是一个 UIView,因此无法生成控件通知。