Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecognizer not work when I animate the UIImageView

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];
                    }
                }];
            }
        }];
    }
}
like image 907
SamirChen Avatar asked Dec 03 '12 15:12

SamirChen


2 Answers

You need to allow user interaction during animation.

UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
like image 96
dariaa Avatar answered Sep 17 '22 15:09

dariaa


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)
like image 29
David West Avatar answered Sep 20 '22 15:09

David West