Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 内部补漆以获得UIImageView_Iphone_Uiimageview - Fatal编程技术网

Iphone 内部补漆以获得UIImageView

Iphone 内部补漆以获得UIImageView,iphone,uiimageview,Iphone,Uiimageview,我正在尝试实现类似于内部触摸的触摸事件ui按钮。我见过一些使用触摸开始:withEvent的代码,但看起来我需要在屏幕上滑动一点手指。实际上我只是想在图像中触摸一下 代码: 问候 您可以使用手势来触摸UIImageView UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Clic

我正在尝试实现类似于
内部触摸的触摸事件
ui按钮。我见过一些使用
触摸开始:withEvent
的代码,但看起来我需要在屏幕上滑动一点手指。实际上我只是想在图像中触摸一下

代码:


问候

您可以使用手势来触摸
UIImageView

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                         initWithTarget:self action:@selector(ClickEventOnImage:)];
        [tapRecognizer setNumberOfTouchesRequired:2];
        [tapRecognizer setDelegate:self];
        //Don't forget to set the userInteractionEnabled to YES, by default It's NO.
        myImageView.userInteractionEnabled = YES;
        [myImageView addGestureRecognizer:tapRecognizer];
执行
单击事件图像
功能

-(void) ClickEventOnImage:(id) sender
{

}

UIImageView
对此臭名昭著,因为默认情况下其
userInteractionEnabled
设置为
NO
。您必须通过执行以下操作来启用它

imageView.userInteractionEnabled = YES;

然后你可以使用手势识别器或普通的
toucks*
方法来处理触摸。

有什么特别的原因不能只使用带有自定义样式和图像的
UIButton
吗?实际上没有,但我想知道是否还有其他方法。如果你不需要
UIButton
提供的功能,您可以直接子类化或使用更简单的
UIControl
<代码>UIControl负责所有讨厌的触摸状态管理,因此您不必这样做。自己动手也意味着你的UI将与所有其他应用稍有不同,这可能会让用户感到困惑。@pmjordan是的,如果你使用的是Interface Builder,并且你想控制图像的布局(方面匹配、方面填充等),你需要使用
UIImage
,这为我节省了很多时间!谢谢那些石头。在handler方法中,您可以通过使用locationInView:(UIView*)view查询手势识别器来获取点击的位置;在scrollview中,返回的坐标包括滚动的内容偏移量。我们可能应该自动释放TapRecognitor,以避免内存泄漏。@Ron:是的,Ron。。我错过了,谢谢你指出…或者可能是在将它添加到imageview之后,我们可以发布它…别忘了添加到接口(.h)
imageView.userInteractionEnabled = YES;