我知道如何为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
已经实现了用户交互并支持细粒度的用户交互。您可以结合 UIImageView
和 UIControl
的功能来实现此目的,因为它们都是 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/