xcode:连续生成向一个方向移动的图像

时间:2012-11-27 07:45:24

标签: iphone xcode

我制作了一个程序,可以在屏幕顶部的随机x坐标处生成图像。然后图像下降到底部。但是,我想每隔几秒钟继续生成新图像(同一图像),这样就好像同一图像的这些副本从顶部不断“下雨”。 (注意:最后,当我继续开发这个应用程序时,我需要随时回忆每个图像的位置,所以我相信我需要每个衍生图像成为数组的一部分。我也相信我必须移动每个图像一步一步,所以我不能依赖动画。)

问题是:如何让所有这些代码每0.5秒重复一次,以便每个新生成的图像都有自己的moveObject计时器。这就像雨滴从顶部落下。

@implementation ViewController {

    UIImageView *_myImage;

}

- (void)viewDidLoad
{
    srand(time(NULL));e
    int random_x_coordinate = rand() % 286;
    CGRect myImageRect = CGRectMake(random_x_coordinate, 0.0f, 40.0f, 40.0f);
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:[UIImage imageNamed:@"flake.png"]];
    myImage.opaque = YES;
    [self.view addSubview:myImage];
    _myImage = myImage;


    //FALLING BIRDS TIMER
    moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveObject) userInfo:nil repeats:YES];


}
    //FALLING BIRDS MOVER
-(void) moveObject {        // + means down and the number next to it is how many pixels y moves down per each tick of the TIMER above
        _myImage.center = CGPointMake(_myImage.center.x, _myImage.center.y +1);
    }

3 个答案:

答案 0 :(得分:0)

您有不同的解决方案:

  • 只需1个计时器,您可以在其中更新UIImageView数组(我更喜欢这个)
  • 子类Imageview并将计时器放在类中,以便每个人都有自己的计时器
  • 也许您可以使用[UIView animateWithDuration...代替计时器,但不确定您是否可以同时使用多个计时器。

答案 1 :(得分:0)

  

问题是:如何让所有这些代码每0.5重复一次   秒,以便每个新生成的图像都有自己的moveObject计时器。

了解Core Animation的粒子效果 - 它们不仅适用于烟雾,雾气和火焰。通过设置粒子发射器并使用图像创建粒子单元,您可以让Core Animation负责整个操作。我没有看到关于该主题的官方文档,但您可以阅读CAEmitterLayer的参考页面以了解其工作原理,然后查看the tutorial on raywenderlich.com

答案 2 :(得分:0)

对每个function raindrop使用此object。只需向point提供move即可:

-(void)raindropAnimation:(CGPoint)dropPoint
{
  raindrop.frame = CGRectMake(dropPoint.x,0,raindrop.frame.size.width,raindrop.frame.size.height) //here y is 0 ie topmost
  [UIView animateWithDuration:2.0 
                      delay:0.0 
                    options:UIViewAnimationCurveEaseInOut 
                 animations:^ {
                      raindrop.frame = CGRectMake(dropPoint.x,dropPoint.y,raindrop.frame.size.width,raindrop.frame.size.height) //will move to point
                 } 
                 completion:^(BOOL finished) {
                     [self perfromSelector:@selector(raindropAnimation:CGPointMake(xnewpointhere,ynewpointhere))];
                 }];