iOS:检测UITmageView for UITouch Events

Hem*_*ang 1 iphone uiimageview uitouch ios

我已经 UIImageView 动态地 添加了一些并用不同的图像填充它,我想要做的是"允许用户为任何设置位置UIImageView ",因为我使用了

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

在那种方法中,我正在做,

NSLog(@"%@",[touches anyObject]);
Run Code Online (Sandbox Code Playgroud)

它返回输出

<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}
Run Code Online (Sandbox Code Playgroud)

注意,在上面的输出中,它显示了 UIImageView 我触摸的对象.但我希望它的对象!

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

我使用下面的代码得到它,但它不会工作.

NSInteger tag=[[[touches anyObject] view] tag]; //It only returns tag of UIView tag
Run Code Online (Sandbox Code Playgroud)

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

预先感谢您的任何帮助!

kus*_*ush 5

干得好:

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

-(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");
    }
} 
Run Code Online (Sandbox Code Playgroud)

或者你也可以这样做:p

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];

    if (touch.view == iv)
    {
        NSLog(@"i got you");
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样:(iv和iv2是2个不同的UIImageView`s)

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

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