识别移动物体中的触摸事件

时间:2011-05-13 12:39:58

标签: iphone core-animation uitouch

我在视图中动态创建了一个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]);
}

请任何人帮忙。

2 个答案:

答案 0 :(得分:1)

这会将图像从左向右移动。和触摸图像,你的touchesBegan方法

来电,你可以在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];

这应该在动画期间启用触摸。

答案 1 :(得分:0)

您需要对图片视图的支持CALayer进行点击测试。所有视图都包含CALayer,CALayer包含模型层和表示层。表示层是您需要访问的层:

[[imageView.layer presentationLayer] hitTest:point]