chis54
chis54

Reputation: 477

ios uiimageview tap gesture not working during animation

I have a UIImageView that is animated and receiving a UITapGestureRecognizer. The problem is while animated, the tap gesture area doesn't move with the view. I can tap the view's original location and the tap gesture is recognized. Anyone know how to have the tap recognized while the view is moving and not at its original position?

Edit 1: This is how I've set up my tap gesture recognizer:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapRecognizer.cancelsTouchesInView = YES;
    tapRecognizer.numberOfTapsRequired = 1;
    tapRecognizer.delegate = (id)self;
    [imageView addGestureRecognizer:tapRecognizer];
    imageView.userInteractionEnabled = YES;

Is there a way to have the gesture recognizer follow my view on its path since my animation lasts a couple seconds? Have I missed a setting?

Edit 2: This is my code to set up my animation:

    double width = backgroundImageView.frame.size.width;
    double height = backgroundImageView.frame.size.height;

    CGMutablePathRef path = CGPathCreateMutable();
    double startX = -imageView.frame.size.width;
    double startY = height/4;
    double endX = 3*width;
    double endY = 0;
    double duration = 40;
    CGPathMoveToPoint(path, NULL, startX, startY);
    CGPathAddLineToPoint(path, NULL, endX, endY);
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.duration = duration;
    animation.path = path;
    animation.repeatCount = HUGE_VALF;
    [imageView.layer addAnimation:animation forKey:@"theAnimation"];

Upvotes: 1

Views: 2230

Answers (1)

chis54
chis54

Reputation: 477

For anyone interested, I solved my problem. Simply use an NSTimer, check and update the UIImageView's frame from the presentation layer by:

CGRect viewLocation = [[[imageView layer] presentationLayer] frame];    
imageView.frame = CGRectMake(viewLocation.origin.x, viewLocation.origin.y, viewLocation.size.width, viewLocation.size.height);

After that, touches work wherever my image view is :-)

Upvotes: 2

Related Questions