需要帮忙。我被这个问题困扰了很多天......我正在做一个触摸杀鸟游戏。当鸟被触摸时,它会随着一系列图像而死去,使其血液飞溅..鸟是静止的,有时它会飞..我可以使用以下代码轻松完成静态部分:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
if ([touches count]==1)
{
location = [mytouch locationInView:self];
UIView *killView = [self hitTest:location withEvent:nil];
if ([killView isKindOfClass:[UIImageView class]]) {
birdKilled = YES;
[self addSubview:[self bloodSplash:killView]];}}}
-(UIImageView *)bloodSplash:(UIView*)killedView {
UIImageView *blood = [[UIImageView alloc]
initWithFrame:CGRectMake(killedView.center.x-100.0/2.0, killedView.center.y-100.0/2.0, 100, 100)];
[killedView setHidden:YES];
blood.image = [bloodExplodes objectAtIndex:2];
[blood setAnimationImages:bloodExplodes];
[blood setAnimationDuration:0.4];
[blood setAnimationRepeatCount:1];
[blood startAnimating];
[self performSelector:@selector(cleanBlood:) withObject:blood afterDelay:.8];
return [blood autorelease];}
但是对于飞行部分,我真的被卡住了!因为我在为飞鸟制作动画时了解到这一点。我需要在 hitTest 中检测它的表示层。我可以使用“UIViewAnimationOptionAllowUserInteraction”成功地做到这一点。但问题是我无法弄清楚如何将bloodSplash函数添加到接触点,因为它将UIImageView返回给CALayer ..这是我的试验......
-(void)touchesBegan:(NSSet *)touches withEvent: (UIEvent *)event{
if ([flyingBird.layer.presentationLayer hitTest:location]) {
CALayer* touchedLayer =
[flyingBird.layer.presentationLayer hitTest:location];
//No Idea what to do next..
}}
因为我真的不太了解图层......谢谢任何建议。