在我的应用程序中,我在viewWillAppear中有此代码。它只是从屏幕顶部到底部为随机对象设置动画。问题是我需要检测碰撞,而到目前为止我所学到的是不可能在任何时候获取动画的坐标。那么如何使用NSTimer来实现呢?还是应该使用NSTimer?我不知道。任何线索将不胜感激。

-(void)viewWillAppear:(BOOL)animated
{
    for (int i = 0; i < 100; i++) {

        p = arc4random_uniform(320)%4+1;

        CGRect startFrame = CGRectMake(p*50, -50, 50, 50);
        CGRect endFrame   = CGRectMake(p*50, CGRectGetHeight(self.view.bounds) + 50,
                                       50,
                                       50);

        animatedView = [[UIView alloc] initWithFrame:startFrame];
        animatedView.backgroundColor = [UIColor redColor];

        [self.view addSubview:animatedView];

        [UIView animateWithDuration:2.f
                              delay:i * 0.5f
                            options:UIViewAnimationCurveLinear
                         animations:^{
                             animatedView.frame = endFrame;
                         } completion:^(BOOL finished) {
                             [animatedView removeFromSuperview];
                         }];

}

最佳答案

您的声明:

是否不可能在任何时候检索坐标
动画

您检查this似乎不正确?

看起来您可以在动画期间使用CALayer的presentationLayer属性提取坐标信息:

CGRect movingFrame = [[yourView.layer presentationLayer] frame];

我将使用此信息不时检查动画过程中是否发生碰撞。因此,我将使用计时器检查碰撞状态,而不是对视图进行动画处理。

关于iphone - iPhone开发:如何使用NSTimer创建动画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12139675/

10-11 22:46
查看更多