Allowing User Interaction for PART of a UIView during animation

408 views Asked by At

I know that the subject of user interaction for UIViews during animations has been well questioned, but I'd like to know whether it is possible to detect whether the touch was for a particular part of the whole UIView.

To start with, my animation code in the UIView is:

[UIView animateWithDuration:15.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{
            self.frame = FINAL_RECT;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];

Hit detection on the viewController:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    BulletView *aBullet;
    for (aBullet in bulletArray) { //bulletArray contains the animating UIViews
        if ([[aBullet.layer presentationLayer] hitTest:touchPoint])
        {
            break;
        }
    }

    [aBullet.layer removeAllAnimations];
    [aBullet removeFromSuperview];
    [bulletArray removeObject:aBullet];

}

The logic being that the view is removed upon a successful hit. This works perfectly, BUT the issue for me is that [[aBullet.layer presentationLayer] hitTest:touchPoint] considers the entire UIView, whereas I need to see if the user clicked a specific area within the whole view. Something like this:

enter image description here

I tried to pass the touchPoint to the UIView to compare it with the frame and see if the touch was within the red square, but this does not work since the frame of the view is already set (by the animation) to the final animation frame.

Your thoughts folks? Thanks in advance..

UPDATE: Still haven't found a solution to this one, but I've temporary worked around the problem by (a) Disabling touches for the main bullet view altogether and (b) Having another view move in front of the bullet, matching its movement (set to the size of the red frame above) which will capture the touch. This works fine, but its obviously not a very memory efficient approach (since I need to use two views instead of one), so it'll be awesome if anyone can come up with a way to properly work this out. Cheers!

0

There are 0 answers