2016-11-10 48 views
0

我正在使用CABasic動畫來移動UIImageViewUIImageView現在沒有檢測到觸摸動畫完成後如何啓用用戶交互?如何在動畫完成的圖像視圖中啓用交互

[ImageView setUserInteractionEnabled:NO]; 
CABasicAnimation *theAnimation; 
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; 
theAnimation.duration=1; 
theAnimation.repeatCount=1; 
theAnimation.autoreverses=NO; 

theAnimation.fromValue=[NSNumber numberWithFloat:0]; 
theAnimation.toValue=[NSNumber numberWithFloat:self.view.frame.size.width/2]; 

theAnimation.removedOnCompletion = NO; 
theAnimation.fillMode = kCAFillModeForwards; 

[CATransaction setCompletionBlock:^{ 
    NSLog(@"finished"); 
    self.view.userInteractionEnabled = true; 
}]; 
[ImageView.layer addAnimation:theAnimation forKey:@"slide"]; 
[CATransaction commit]; 
+3

你設置''[ImageView setUserInteractionEnabled:否];''但'self.view.userInteractionEnabled = true';你應該嘗試'[ImageView setUserInteractionEnabled:YES];'而不是 – LoVo

+0

'[CATransaction setCompletionBlock:^ {NSLog(@「finished」); 自我。 ImageView.userInteractionEnabled = true; }];' – Mahesh

+0

它不能正常工作 – Sumi

回答

0

在上面的代碼,而不是

self.view.userInteractionEnabled = true; 

使用本

ImageView.userInteractionEnabled = YES; 
0

你可以試試這個。

BOOL animationPause; 

ImageView = [[UIImageView alloc]initWithFrame:CGRectMake(250, 300, 100, 100)]; 

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageViewClicked)]; 
    singleTap.numberOfTapsRequired = 1; 
    [ImageView addGestureRecognizer:singleTap]; 

    [self.view addSubview:ImageView]; 

    ImageView.image = [UIImage imageNamed:@"earth.jpeg"]; 

    [UIView animateWithDuration:1.0 animations:^{ 
     for (int i = 0; i<5; i++) { 
      ImageView.frame = CGRectMake(ImageView.frame.origin.x, ImageView.frame.origin.y+i+5, ImageView.frame.size.width, ImageView.frame.size.height) ; 
      if (i==4) { 
       ImageView.userInteractionEnabled = YES; 

      } 
if (animationPause == YES) 
       [UIView setAnimationDidStopSelector:@selector(animateStop)]; 
      else 
       [UIView setAnimationDidStopSelector:@selector(Resume)]; 
     } 

     } 
    }]; 

沒有必要在你的場景中使用CABasicAnimation實際上有一些最初的幀圖像視圖,並已添加的手勢什麼的。正確的,當你動畫你的形象將去x或y根據動畫的要求。一個仍在初始幀的動態圖像視圖幀。所以你的點擊事件方法會被調用。

但是,當你點擊特定的框架是否有imageView或者你不會注意到你的方法會被調用。

您管理了動畫暫停暫停和恢復動畫。 希望它能起作用。如果你仍然有任何查詢讓我通知

+0

其實我想暫停和恢復動畫..是否有可能在你的代碼? – Sumi

+0

你可以做到男人。只需要一些你想暫停或恢復的邏輯。是的,你也可以使用這個代碼。但是如果你想使用你的代碼,不要擔心只是在做動畫的時候改變你的imageView框架。 –

+0

我希望你得到它 –