0

需要帮忙。我被这个问题困扰了很多天......我正在做一个触摸杀鸟游戏。当鸟被触摸时,它会随着一系列图像而死去,使其血液飞溅..鸟是静止的,有时它会飞..我可以使用以下代码轻松完成静态部分:

-(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..
  }}

因为我真的不太了解图层......谢谢任何建议。

4

1 回答 1

0

为了将图像视图添加到您的飞鸟视图中,您需要找到正确的视图。UIView 由 CALayer 支持,并且该层将 UIView 作为其委托。当您制作动画时,您需要在表示层上进行 hitTest,然后返回模型层,因为那是与您相关的层。从那里您可以通过查询图层的委托找到相应的 UIView。所以看起来像这样:

CALayer *hitLayer = [[flyingBird.layer.presentationLayer hitTest:location] modelLayer];
UIView *hitView = [hitLayer delegate];

这应该是您可以添加动画 UIImageView 的视图。

于 2012-09-16T10:08:08.663 回答