动画停止后一一删除CALayers

发布于 2024-11-27 21:11:27 字数 590 浏览 0 评论 0原文

当用户单击“飞入”时,我想制作一堆硬币 - >硬币会沿着弯曲的路径飞行并排列成一堆。我使用了 CALayer:

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 时,这是正确的吗?如何像示例一样一层一层地删除层?

太感谢了!

I want to make a stack of coin, when user click "Fly in" -> coin'll fly in a curved path and arrange in a stack. I used CALayer:

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];

I've done with animation in curved path but if i add my coinLayer in this way, then how can i remove CALayer if i don't add it in an array.

For example, i have a stack of number, i add 1,2,3,4,5,6,7,8,9 in the stack. When remove 4 numbers, it will do from 9 down to 8... down to 6 one by one. In my code, is that correct when i add CALayer in view's layer? How can i remove layers one by one as same as example?

Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-12-04 21:11:27

与其创建和删除 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 对象,它比不断创建新的 CALayer 更加资源友好。

Rather than creating and deleting CALayer objects it's better to store them in an array and simply set their hidden property whenever you need them.

in the header:

    NSMutableArray* coins;

In the m file:

-(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]];

When the animation finishes simply set the coin's hidden property to be true.

coin.hidden = YES;

I use this method to handle thousands of CALayer objects at once and it's much more resource friendly than constantly creating new CALayers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文