【问题标题】:Randomizing images while floating up the screen在屏幕上浮动时随机化图像
【发布时间】:2014-03-12 02:12:09
【问题描述】:

在我的应用程序中,我计划添加气球,这些气球会在屏幕上浮动并在遇到尖峰时弹出。我需要帮助实现气球漂浮在屏幕上并使其随机生成生成点。我还需要添加代码,逐渐使气球在屏幕上漂浮得更快,数量更多。

关于在哪里可以找到教程/帮助的任何建议。

【问题讨论】:

  • 这并不完全符合 SO 上的编程问题。无论如何,很多人都觉得 Ray Wenderlich 很有帮助……raywenderlich.com/tutorials
  • 是的,只是真的不知道如何询问与此相关的编码 Q,但感谢您的链接!

标签: ios iphone image performance floating


【解决方案1】:
  1. 在.h文件中设置一个int,可能是int balloonMovement
  2. 将所有气球图像链接到大括号之间的 .h 作为 IBOutlets

IBOutlet UIImageView *balloon1; IBOutlet UIImageView *balloon2;

  1. 设置另一个int randomPosition 并使用随机数生成器设置气球在屏幕上的位置,可能是当用户单击开始时,或者发生某些事情时。

你可以在 viewDidLoad 中有这个:

RandomPosition = arc4random()%248; //the number of possibilities
RandomPosition = RandomPosition + 36; //the new position 
balloon1.center = CGPointMake(RandomPosition, RandomPosition); //make position

这会使它们随机出现在屏幕上。

然后,为了让它们向上浮动,设置int upMovement;NSTimer *movement;,并每隔一段时间设置一次,它们就会向上移动。

使用以下代码:

    movement = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moving) userInfo:nil repeats:YES];

这表示,每 0.05 秒,我们要激活一次运动。

然后在.h中创建一个气球运动的方法——-(void)balloonMovement;

并在.m中填写其信息 -->

-(void)balloonMovement {

    balloon1.center = CGPointMake(balloon1.center.x, balloon1.center.y + upMovement);

}

另一个用于实际移动气球,并设置您希望它们向上浮动的条件,即用户剪断绳子?

-(void)balloonMoving {
     upMovement = upMovement + 0.1 //this will make it gradually faster 

     if (//user cuts string code) {
        [self balloonMovement];
     }
}

这应该可行。显然它非常简单。您将不得不对其进行自定义。但这应该为您提供基础知识。希望它有所帮助。

还有一件事**

要让它们在屏幕上移动时改变位置,您必须设置一个 int sideMovement,并以与 upMovement 相同的方式添加它。但我会把它留给你去弄清楚。

【讨论】:

  • 我还应该如何链接 UiImageViews?
  • UIView 上的气球! @约翰保罗
  • 我不希望气球在它们生成时出现在 UIView 上,我希望它在屏幕上没有它的情况下随机生成! @user3363493
  • 然后使用 bool .hidden 并在 viewDidLoad 中声明它。 IE。 balloons.hidden = YES,然后,当您希望它们生成时,例如, balloons.hidden = NO,以及随机数生成器。
  • 您总是可以将气球绘制到视图上,但我并不完全了解这个过程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多