2011-08-03 45 views
0

当用户点击“Fly in” - > coin'll以曲线形式飞行并排列堆叠时,我想要制作一叠硬币。我用的CALayer:动画停止后移除CALayers

CALayers *coinLayer = [CALayers layer]; 
coinLayer.backgroundColor = [UIColor clearColor].CGColor; 
coinLayer.contents = (id)[UIImage imageNamed:@"head coin.png"].CGImage; 
coinLayer.frame = CGRectMake(100, 500 - (10*coin), 55, 21); 
coin = coin + 1; 
[self.view.layer addSublayer:coinLayer]; 

我与曲线路径动画完成,但如果我这样加我coinLayer,那我怎么才能去除CALayer的,如果我没有在数组中添加它。

例如,我有一堆数字,我在堆栈中添加1,2,3,4,5,6,7,8,9。当删除4个数字时,它会从9到8逐渐减少到6。在我的代码中,当我在视图的图层中添加CALayer时,这是否正确?如何像示例一样一个一个地移除图层?

非常感谢!

+0

任何人都可以帮助我吗?我使用“removeFromSuperLayer”,但我只删除了我刚刚添加的一个图层。但它之前并未与该层一起工作。如果我使用方法“insertSublayers:atIndex:”,可以将图层添加到视图的图层中。但我找不到可以在索引处移除图层的方法。如果有人解决了同样的问题,请帮助我吗? – longnd

回答

1

而不是创建和删除CALayer对象,最好将它们存储在一个数组中,并只需在需要时设置它们的隐藏属性。

在标题:

NSMutableArray* coins; 

在M档:

-(void)newCoin; 
{ 
    //create the array if it doesn't already exist - could add this to your init 
    if(!coins) 
    { 
     coins = [NSMutableArray array]; 
    } 
    for(CALayer* aCoin in coins) 
    { 
     //find the first hidden coin and use it 
     if(aCoin.hidden) 
     { 
      //reset the coins position to where you want the "new" coin 
      return; 
     } 
    } 
    //didn't find any unused coins - make a new one 
    [coins addObject:[self createCoin]]; 

当动画完成简单的设置硬币的隐藏属性为true。

coin.hidden = YES; 

我用这个方法同时处理成千上万的CALayer对象和它的更比资源不断地创造新的CALayers友好。