iphone - 为 UIImageView 实现 UITouchDown

标签 iphone xcode ios4 uiimageview

我知道如何为UIImageView实现touchesBegan

是否可以为UIImageView实现UITouchDown?(我知道我可以使用touchesBegan而不是UITouchDown > 但我想实现 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 已经实现了用户交互并支持细粒度的用户交互。您可以结合 UIImageViewUIControl 的功能来实现此目的,因为它们都是 UIView 的子类。

要获得此行为,请将 UIImageView 对象作为 subview 添加到 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];

关于iphone - 为 UIImageView 实现 UITouchDown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4930449/

相关文章:

iphone - Objective-C 查找和替换字符串问题

ios - 使用 Apple ID 登录

ios - TabBarController 的自动布局问题

xml - 如何使 Xcode 使用自定义 DTD 进行自动完成?

objective-c - 在 Xcode 中使用 Cocoa Framework 显示窗口

iphone - 创建新的私钥会影响已经在 App Store 中的应用程序吗?

iphone - 将极其基本的 objective-c 程序连接到 iPhone 应用程序

ios - 外部硬件指纹扫描仪和 iPhone 设备集成

iphone - iPhone/iPad 应用程序启动时发出警报

ios - 架构 i386 的重复符号 - 添加自定义框架时出错