【问题标题】:xcode: Continuously generating images that move in a directionxcode:连续生成沿一个方向移动的图像
【发布时间】:2012-11-27 07:45:23
【问题描述】:

我制作了一个程序,可以在屏幕顶部的随机 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);
    }

【问题讨论】:

    标签: iphone xcode


    【解决方案1】:

    你有不同的解决方案:

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

    【讨论】:

    • 谢谢!这份清单很有帮助。我正在考虑尝试第二个对象..但我不确定如何对 imageview 进行子类化。过程简单吗?
    • 只需创建一个类,使其继承自 UIImageView 而不是 NSObject 并添加您需要的代码
    • 抱歉所有问题。现在我已经创建了新类(称为 BirdImageView)并在其中放置了计时器代码,如何将我的 UIImageView 代码引用到 BirdImageView 子类?感谢大家的帮助!
    • 你需要一个#import "BirdImageView.h",然后使用 BirdImageView 代替 UIImageView:BirdImageView *img = [[BirdImageView alloc] ...
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      为每个raindrop object 使用这个function。只需将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))];
                       }];
      

      【讨论】:

      • 非常感谢这段代码!不过,我有点不确定如何实现它。所以 NSTimer 会像这样连接到这个?: moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(raindropAnimation) userInfo:nil repeats:YES];
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2012-03-03
      • 2013-04-04
      相关资源
      最近更新 更多