10

When I want to animate the UIImageView, the UITapGestureRecognizer added to it can not work. WHY???

-(void) testTap:(id)sender {
    NSLog(@"Test tap...");
}

-(void) testSlide {
    UITapGestureRecognizer* testTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(testTap:)] autorelease];
    testTap.numberOfTapsRequired = 2;

    UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tip_slide"]] autorelease];
    [imageView setFrame:CGRectMake(40, 40, 200, 200)];
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;
    [imageView addGestureRecognizer:testTap];

    [self.view addSubview:imageView];


    // When I add the following code, the UITapGestureRecognizer will not work. WHY???
    imageView.alpha = 0;
    CGAffineTransform t = imageView.transform;
    if (CGAffineTransformIsIdentity(t)) {
        UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut;
        [UIView animateWithDuration:1.0 delay:0 options:options animations:^{
            imageView.alpha = 1.0;
        } completion:^(BOOL finished) {
            if (finished) {
                [UIView animateWithDuration:1.0 delay:2.0 options:options animations:^{
                imageView.alpha = 0.4;
                } completion:^(BOOL finished) {
                    if (finished) {
                        [imageView removeFromSuperview];
                    }
                }];
            }
        }];
    }
}
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
SamirChen
  • 1,209
  • 1
  • 12
  • 22

2 Answers2

29

You need to allow user interaction during animation.

UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
dariaa
  • 6,285
  • 4
  • 42
  • 58
  • 1
    OMG. Spent so many hours to figure out why it doesn't work. Your answer is life saver! – GeneCode Jan 06 '17 at 04:14
  • Also worth noting: Animating the alpha value of a view to 0 will cause interaction to fail. To get around this I've made mine a low value `view.alpha = 0.05` rather than 0. – Tasik Jun 19 '19 at 23:05
  • 1
    This is the solution for Swift 5: `UIView.animate(withDuration: 0.2, delay: 0.0, options: [.allowUserInteraction], animations: { self.backgroundColor = newColor })` – Maxim Skryabin Jul 23 '20 at 15:45
3

Just to add to this for Swift...something like this will work very well... Use .AllowUserInteraction.

UIView.animateWithDuration(0.4, delay: 1.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 20, options: UIViewAnimationOptions.AllowUserInteraction, animations:
                {self.frame = originalFrame}, completion: nil)
David West
  • 1,550
  • 1
  • 18
  • 31
  • 1
    if you have many animation options you can pass them like this : ``` UIView.animateWithDuration(1.0, delay: 0.2, options: [.CurveEaseOut, .Repeat, .Autoreverse, .AllowUserInteraction], animations: { self.leftArrowImageView.alpha = 0.0 }, completion: nil)``` – Mallox Jul 08 '16 at 11:42