2012-12-03 60 views
8

当我想动画UIImageViewUITapGestureRecognizer添加到它不能工作。为什么???UITapGestureRecognizer不起作用当我动画UIImageView

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

回答

22

您需要允许用户在动画过程中进行交互。

UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction; 
+0

OMG。花了很多时间来弄清楚为什么它不起作用。你的答案是拯救生命! – GeneCode

3

只需添加到这个雨燕为...这样的事情会工作得很好...... 使用.AllowUserInteraction

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

如果有许多动画选项可以通过他们这样的: '''UIView.animateWithDuration(1.0, 延迟:0.2, 选项:[.CurveEaseOut,.Repeat,.Autoreverse,.AllowUserInteraction], 动画:{ self.leftArrowImageView.alpha = 0.0 },完成:无)''' – Mallox