2012-12-03 12 views
8

Wenn ich das UIImageView animieren möchte, kann das UITapGestureRecognizer, das hinzugefügt wird, nicht funktionieren. WARUM???UITapGestureRecognizer funktioniert nicht, wenn ich das UIImageView animiere

-(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]; 
        } 
       }]; 
      } 
     }]; 
    } 
} 

Antwort

22

Sie müssen Benutzerinteraktion während der Animation zulassen.

UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction; 
+0

OMG. Habe so viele Stunden gebraucht, um herauszufinden, warum es nicht funktioniert. Ihre Antwort ist Lebensretter! – GeneCode

3

Nur um diese für Swift hinzuzufügen ... so etwas wie dies sehr gut funktioniert ... Verwenden .AllowUserInteraction.

UIView.animateWithDuration(0.4, delay: 1.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 20, options: UIViewAnimationOptions.AllowUserInteraction, animations: 
       {self.frame = originalFrame}, completion: nil) 
+1

, wenn Sie viele Animationsoptionen haben, können Sie sie wie folgt passieren: '' 'UIView.animateWithDuration (1,0, Verzögerung: 0,2, Optionen: [.CurveEaseOut, .Wiederholen, .Autoreverse, .AllowUserInteraction], Animationen: { self.leftArrowImageView.alpha = 0,0 }, Abschluss: null) '' ' – Mallox