Implement UITouchDown for UIImageView(为 UIImageView 实现 UITouchDown)
问题描述
我知道如何为 UIImageView
实现 touchesBegan
I know how to implement touchesBegan
for UIImageView
是否可以为 UIImageView
实现 UITouchDown
?(我知道我可以使用 touchesBegan
而不是 UITouchDown
但我想实现 UITouchDown
)
is it possible to implement UITouchDown
for UIImageView
?(i know that i can use touchesBegan
instead of UITouchDown
but i want to implement UITouchDown
)
推荐答案
使用 UIButton.
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:someUIImage forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
UIButton 的替代品(更长的方法)
UIControl
已经实现了用户交互并支持细粒度的用户交互.您可以结合 UIImageView
和 UIControl
的功能来实现这一点,因为它们都是 UIView
的子类.
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
.
要获得此行为,请将 UIImageView 的对象作为子视图添加到 UIControl
以便完全覆盖图像.然后使用 addTarget:action:forControlEvents:
.这是一个例子:
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:
// 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];
这篇关于为 UIImageView 实现 UITouchDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 UIImageView 实现 UITouchDown
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01