【问题标题】:How to get the position of oldest object made during animation?如何获取动画期间制作的最旧对象的位置?
【发布时间】:2014-07-29 03:32:48
【问题描述】:

我每秒钟都制作一个名为 FrenchFrie 的炸薯条的 UIImageView。我还让他们通过动画沿着屏幕移动。

-(void)FrenchFrieRain{
    FrenchFrie = [[UIImageView alloc] initWithFrame:CGRectMake(randomX,0,30,30)];
    FrenchFrie.image = [UIImage imageNamed:@"FrenchFriesMCDonalds"];
    FrenchFrie.userInteractionEnabled = YES;
    [self.view addSubview:FrenchFrie];
    [FrenchFrieArray addObject: FrenchFrie];
    [self letFrenchFrieFall];
}

-(void)letFrenchFrieFall {
    [UIView animateWithDuration:10.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        FrenchFrie.frame = CGRectMake(randomX, (int)[[UIScreen mainScreen] bounds].size.height + 40, FrenchFrie.frame.size.width, FrenchFrie.frame.size.height);
    } completion:^(BOOL finished){
        [[FrenchFrieArray objectAtIndex:0] removeFromSuperview];
}];

}

我需要动画期间薯条的位置。我使用以下代码执行此操作:

FrenchFrieFrame = [[FrenchFrie.layer presentationLayer] frame];

从 NSLogging 我知道,通过这段代码,我得到了最新薯条的位置,但我需要最旧薯条的位置。我如何才能获得最古老的法式炸薯条的位置? 任何帮助将不胜感激!

【问题讨论】:

    标签: ios objective-c presentation-layer


    【解决方案1】:

    您正在跟踪您在FrenchFrieArray 中创建FrenchFries 的顺序。因此,索引0 处的项目将始终是最旧的FrenchFrie。你可以使用:

    FrenchFrieFrame = [[FrenchFrieArray[0].layer presentationLayer] frame];
    

    为此,除了从视图中删除之外,您还应该从数组中删除 FrenchFrie

        [[FrenchFrieArray objectAtIndex:0] removeFromSuperview];
        [[FrenchFrieArray removeObjectAtIndex:0];
    

    【讨论】:

    • 薯条从屏幕上掉下来,我想知道薯条和屏幕底部的篮子是否碰撞。如果他们不碰撞最后一个薯条应该从屏幕上掉下来并消失,否则它应该消失在篮子的中心。我无法制作 OldestFrenchFrie,因为在最老的从屏幕上掉下来后,第二个被创建的就变成了最老的。
    • 对不起,做演员:[((FrenchFrie*)FrenchFrieArray[0]).layer[[[FrenchFrieArray[0] layer] ...
    • FrenchFrieFrame = [[FrenchFrieArray[0].layer presentationLayer] frame];给出了一个错误。我试过 OldestFrenchFrie = [FrenchFrieArray objectAtIndex:0]; FrenchFrieFrame = [[OldestFrenchFrie.layer presentationLayer] 框架];这没有给出错误,但是如果我 NSLog 炸薯条的位置,我会得到 x-co = 0 和 Y-co = 0。
    • 此外,当我将 FrenchFrie 添加到数组时,数组的值在调试区域中保持为零。
    猜你喜欢
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    相关资源
    最近更新 更多