繁体   English   中英

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

[英]Recognize the touch event in the moving object

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

请任何人都可以提供帮助。

这将使图像从左向右移动。 在触摸图像时,您的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];

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

您需要对图像视图的支持CALayer进行命中测试。 所有视图均包含CALayer,而CALayer包含模型层和表示层。 表示层是您需要访问的层:

[[imageView.layer presentationLayer] hitTest:point]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM