2011-05-13 4 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 메서드

이 호출되며 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 : animation : completion :이 필요합니다. 현재 방법 바꾸기

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

애니메이션 중에 터치가 가능해야합니다.

+0

안녕하세요 Mistry, 답장을 보내 주셔서 감사합니다. 나는 또한 당신의 코드로 시도했지만 작동하지 않습니다. 내 질문을 편집했습니다. 코드를보고 내가 실수를하고 있음을 알려주십시오. – MohanVydehi

0

이미지보기의 백업 CALayer에 대해 히트 테스트를 수행해야합니다. 모든 뷰는 CALayer를 포함하고 CALayer는 모델 레이어와 프리젠 테이션 레이어를 모두 포함합니다. 프리젠 테이션 레이어는 액세스해야하는 레이어입니다.

[[imageView.layer presentationLayer] hitTest:point] 
관련 문제