动画期间UITapGestureRecongnizer失败

时间:2014-12-22 08:09:17

标签: ios objective-c iphone animation uigesturerecognizer

我使用以下代码生成可触摸的图像。

我在这里看了一些帖子,说动画期间无法使用UIButton所以我改为使用点按手势。我还在选项中添加了UIViewAnimationOptionAllowUserInteraction。但我仍然无法使其发挥作用。我可以知道原因和任何解决方案吗?

hit1 = [[UIImageView alloc]initWithFrame:CGRectMake(539,227,50,50)];
hit1.image=[UIImage imageNamed:@"roles.png"];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hitenermy:)];
[hit1 addGestureRecognizer:singleTap];
[hit1 setMultipleTouchEnabled:YES];
[hit1 setUserInteractionEnabled:YES];
hit1.userInteractionEnabled=YES;


[self.view addSubview:hit1];
[hit1 addGestureRecognizer:singleTap];
UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;

[UIView animateWithDuration:15.0
                      delay:1.0
                    options:options
                 animations:^{
                     hit1.center = CGPointMake(hit1.center.x-210, hit1.center.y-60);

                 }
                 completion:^(BOOL finished){

                 }];

1 个答案:

答案 0 :(得分:1)

您可以使用- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event来获取触摸事件。 我已经展示了一个imageView,但是如果存在多个imageView,那么你可以使用tag来区分它们。

您可以从UIButton can't be touched while animated with UIView animateWithDuration

获得更多信息
- (void)viewDidLoad
    {
        [super viewDidLoad];

        hit1 = [[UIImageView alloc]initWithFrame:CGRectMake(539,227,50,50)];
        hit1.image=[UIImage imageNamed:@"roles.png"];

        [self.view addSubview:hit1];

        UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;

        [UIView animateWithDuration:15.0
                              delay:1.0
                            options:options
                         animations:^{
                              hit1.center = CGPointMake(hit1.center.x-210, hit1.center.y-60);
                         }
                         completion:^(BOOL finished){

                         }];
    }



     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        {
            UITouch *touch = [touches anyObject];
            CGPoint touchLocation = [touch locationInView:self.view];
            for (UIImageView *imageView in self.view.subviews)
            {
                if ([imageView.layer.presentationLayer hitTest:touchLocation])
                {
                    NSLog(@"Clicked");
                    break;
                }
            }
        }