2012-12-03 17 views
8

Khi tôi muốn tạo ảnh động UIImageView, số UITapGestureRecognizer được thêm vào không thể hoạt động. TẠI SAO???UITapGestureRecognizer không hoạt động khi tôi tạo hiệu ứng cho 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]; 
        } 
       }]; 
      } 
     }]; 
    } 
} 

Trả lời

22

Bạn cần cho phép tương tác người dùng trong khi hoạt ảnh.

UIViewAnimationOptions options = UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction; 
+0

OMG. Đã dành rất nhiều giờ để tìm ra lý do tại sao nó không hoạt động. Câu trả lời của bạn là cuộc sống tiết kiệm! – GeneCode

3

Chỉ cần để thêm vào này cho Swift ... một cái gì đó như thế này sẽ làm việc rất tốt ... Sử dụng .AllowUserInteraction.

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

nếu bạn có nhiều lựa chọn hình ảnh động, bạn có thể vượt qua chúng như thế này: '' 'UIView.animateWithDuration (1.0, chậm trễ: 0.2, lựa chọn: [.CurveEaseOut, .Repeat, .Autoreverse, .AllowUserInteraction], hình ảnh động: { self.leftArrowImageView.alpha = 0.0 }, hoàn thành: nil) '' ' – Mallox

Các vấn đề liên quan