本文介绍了iPhone开发:如何使用的NSTimer创建一个动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我在这viewWillAppear中code。它只是动画从屏幕底部的顶部随机对象。问题是我需要检测碰撞,我学到到目前为止它是不可能在任何时候为动画检索坐标。所以我怎么能使用的NSTimer实现它?或者我应该使用的NSTimer?我无法弄清楚。任何线索将AP preciated。

   - (无效)viewWillAppear中:(BOOL)动画
{
    的for(int i = 0; I< 100;我++){        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的页头] initWithFrame:方法startFrame将];
        animatedView.backgroundColor =的UIColor redColor]        [self.view addSubview:animatedView];        [UIView的animateWithDuration:2.F
                              延迟:我* 0.5F
                            选项​​:UIViewAnimationCurveLinear
                         动画:^ {
                             animatedView.frame = endFrame;
                         }完成:^(BOOL完){
                             [animatedView removeFromSuperview]
                         }];}


解决方案

您的语句:

seems incorrect did you check this?

It looks like you can use the CALayer's presentationLayer property to extract coordinates info during an animation:

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

I will use this info to check time to time if there is a collision during the animation. So I will use the timer to check the collision status not for animating the views.

这篇关于iPhone开发:如何使用的NSTimer创建一个动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:03