2011-05-13 51 views
0

我在视图中动态创建了UIImageView并进行了动画处理(从一个位置移动到另一个位置)。我写了触摸事件并尝试识别图像视图上的触摸。但是,当图像具有动画效果时,它无法识别触摸事件。识别移动对象中的触摸事件

但是,如果我触摸了我添加图像视图的初始位置,它正在识别,但动画触摸不起作用。

UIImageView *birdImage = [[UIImageView alloc] initWithFrame: CGRectMake(-67, 100, 67, 52)]; 
birdImage.image = [UIImage imageNamed: @"Flying_bird.png"]; 
birdImage.tag = 1000; 
birdImage.userInteractionEnabled = YES; 
[birdImage setContentMode: UIViewContentModeScaleAspectFit]; 
[self.view addSubview: birdImage]; 

[UIView animateWithDuration:10.0 delay:0.0 options: ViewAnimationOptionAllowUserInteraction animations:^{ 
      birdImage.frame = CGRectMake(547, 100, 67, 52); 
     } completion:nil]; 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    NSLog(@"Tag: %d", [[touch view] tag]); 
} 

请任何人都可以在这帮助。

回答

1

这会将图像从左到右移动。并在触摸图像,触摸开始方法

调用,你可以做任何事情在你的touchesBegan事件。

UIImage *image = [UIImage imageNamed:@"image.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
imageView.frame = CGRectMake(-1024, 0, 1024, 768); 
[self.view addSubview:imageView]; 
[imageView release]; //Your imageView is now retained by self.view 

您可能需要此方法-animateWithDuration:delay:options:animations:completion :.用当前方法替换当前方法

[UIView animateWithDuration:10.0 
         delay:0.0 
        options: UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         imageView.frame = CGRectMake(0, 0, 1024, 768); 
        } 
        completion:nil]; 

这应该在动画过程中启用触摸。

+0

嗨Mistry,谢谢你的回应。我也试过你的代码,但没有工作。我编辑了我的问题,请查看代码并让我知道我在哪里犯错误。 – MohanVydehi 2011-05-16 11:20:14

0

您需要对图像视图的背景CALayer进行命中测试。所有视图都包含CALayer,CALayer同时包含模型图层和表示图层。表示层是您需要访问的表示层:

[[imageView.layer presentationLayer] hitTest:point]