2

I'm trying to detect a touch whilst my UIImageView is animating. The touch detection works before the animation starts, and once it stops, but not during.

I've tried adding UIViewAnimationOptionAllowUserInteraction, but it seems to have no effect at all!

Could anyone point me in the right direction?

Code:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch]);
    if ([touch view] == itemCoke)
    {
        NSLog(@"Coke Touched");
    }

}

- (IBAction)removeItemButton:(id)sender {
    NSLog(@"Pushed");


    [UIView animateWithDuration:5 
                          delay:0.0         
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^
                             {
                                 itemCoke.transform = CGAffineTransformRotate(itemCoke.transform, (M_PI*-0.5));
                                 itemCoke.frame = CGRectMake(50, 50, 50, 50);
                             }
                     completion:^(BOOL finished){}];   
}

Thanks for any advice!

NJones
  • 27,139
  • 8
  • 70
  • 88
Synchro
  • 23
  • 1
  • 4

2 Answers2

9

The reason the interactions are not working is that essentially the UIImageView is not where it appears to be. Only the view's CALayer's presentation layer is being animated around the screen. The view has already arrived at it's destination immediately. This makes interaction much harder. You will likely find this answer helpful.

Community
  • 1
  • 1
NJones
  • 27,139
  • 8
  • 70
  • 88
0

this should do the trick

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if ([[self.layer presentationLayer] hitTest:point]) {
    return self;
}
return [super hitTest:point withEvent:event];}

write it in your uiview

Taha Selim Bebek
  • 45
  • 1
  • 8
  • 16
  • Be careful. If the user taps on the spot to which the view is animating, this will return a false positive (because while the `presentationLayer` hit test will fail, the `super` one will use the destination frame for hit test purposes. – Rob Feb 07 '21 at 08:38