iphone - iOS : detect UIImageView for UITouch Events

标签 iphone ios uiimageview uitouch

我添加了一些 UIImageView 动态并用不同的图像填充它,我想做的是“允许用户为任何 UIImageView 设置位置”,为此我使用了

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    //Here I want the object of particular UIImageView on which user touched.
}

在我正在做的那种方法中,
NSLog(@"%@",[touches anyObject]);

它返回输出
<UITouch: 0x68b95e0> phase: Began tap count: 1 window: <UIWindow: 0x68875d0; frame = (0 0; 320 480); layer = <UIWindowLayer: 0x68b6470>> view: <UIImageView: 0x6a74cf0; frame = (83.7763 83.7763; 182.447 182.447); transform = [0.968912, -0.247404, 0.247404, 0.968912, 0, 0]; alpha = 0.8; opaque = NO; tag = 3; layer = <CALayer: 0x6a74980>> location in window: {161, 230} previous location in window: {161, 230} location in view: {52.7761, 105.448} previous location in view: {52.7761, 105.448}

请注意,在上面的输出中,它显示了我的 UIImageView 我触摸的对象。但我想要那个对象!!!!

我想要我的 UIImageView哪个用户触摸了?,我已经设置了属性userInteractionEnabled=YES所以问题不在于它!

我使用下面的代码来得到它,但它不会工作。
NSInteger tag=[[[touches anyObject] view] tag]; //It only returns tag of UIView tag

我谷歌它,但没有解决方案!

预先感谢您的任何帮助!

最佳答案

干得好:

这仅适用于一个 ImageView ,您可以通过相同的 if 语句检测另一个 ImageView 。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject]; 
    CGPoint touch_point = [touch locationInView:self.view];

    if (![imageView pointInside:touch_point withEvent:event]) 
    {
        NSLog(@"point inside imageview");
    }
} 

或者你也可以这样做:p
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];

    if (touch.view == iv)
    {
        NSLog(@"i got you");
    }
}

像这样:(iv 和 iv2 是 2 个不同的 UIImageView`s)
if (touch.view == iv)
{
    NSLog(@"i got you");
}

if (touch.view == iv2)
{
    NSLog(@"i got you too :p");
}

关于iphone - iOS : detect UIImageView for UITouch Events,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9461277/

相关文章:

ios - 在初始 View Controller 实例化时以编程方式绘制 ImageView 边界

iphone - 如何将 UIImage 插入 UITextView

ios - 为什么当禁用 allowedEditing 时,iPhone 上的相机拍摄的全屏照片不会显示在 UIImageview 中?

iphone - SimpleAudioEngine 需要时间来播放声音

iOS 模拟器不使用与 OS X 相同的布局

iPhone 设置应用程序和键盘控制?

ios - 通过 iTunes Connect 请求创建的所有沙盒测试人员 "Payment Information Required"

iphone - iOS 和红外线

iphone - 在App Store中搜索应用

ios - 如何将 CUPS 安装到 iOS 设备上?