2011-05-13 91 views
0

我在視圖中動態創建了UIImageView並進行了動畫處理(從一個位置移動到另一個位置)。我寫了觸摸事件並嘗試識別圖像視圖上的觸摸。但是,當圖像具有動畫效果時,它無法識別觸摸事件。識別移動對象中的觸摸事件

但是,如果我觸摸了我添加圖像視圖的初始位置,它正在識別,但動畫觸摸不起作用。

UIImageView *birdImage = [[UIImageView alloc] initWithFrame: CGRectMake(-67, 100, 67, 52)]; 
birdImage.image = [UIImage imageNamed: @"Flying_bird.png"]; 
birdImage.tag = 1000; 
birdImage.userInteractionEnabled = YES; 
[birdImage setContentMode: UIViewContentModeScaleAspectFit]; 
[self.view addSubview: birdImage]; 

[UIView animateWithDuration:10.0 delay:0.0 options: ViewAnimationOptionAllowUserInteraction animations:^{ 
      birdImage.frame = CGRectMake(547, 100, 67, 52); 
     } completion:nil]; 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    NSLog(@"Tag: %d", [[touch view] tag]); 
} 

請任何人都可以在這幫助。

回答

1

這會將圖像從左到右移動。並在觸摸圖像,觸摸開始方法

調用,你可以做任何事情在你的touchesBegan事件。

UIImage *image = [UIImage imageNamed:@"image.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
imageView.frame = CGRectMake(-1024, 0, 1024, 768); 
[self.view addSubview:imageView]; 
[imageView release]; //Your imageView is now retained by self.view 

您可能需要此方法-animateWithDuration:delay:options:animations:completion :.用當前方法替換當前方法

[UIView animateWithDuration:10.0 
         delay:0.0 
        options: UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         imageView.frame = CGRectMake(0, 0, 1024, 768); 
        } 
        completion:nil]; 

這應該在動畫過程中啓用觸摸。

+0

嗨Mistry,謝謝你的迴應。我也試過你的代碼,但沒有工作。我編輯了我的問題,請查看代碼並讓我知道我在哪裏犯錯誤。 – MohanVydehi 2011-05-16 11:20:14

0

您需要對圖像視圖的背景CALayer進行命中測試。所有視圖都包含CALayer,CALayer同時包含模型圖層和表示圖層。表示層是您需要訪問的表示層:

[[imageView.layer presentationLayer] hitTest:point]