我需要滚动大于屏幕尺寸的背景图像并使其无限循环
我正在编写一个 ipad 应用程序,我已将其方向锁定为横向模式。 我想要一个 100 像素高但宽度是 ipad 屏幕宽度 4 倍的 PNG 文件。
我想要的是这个背景图像显示在屏幕上然后自动开始 在计时器上滚动。我已确保该图像的开始部分和结束部分匹配。 一旦到达图像的末尾,我希望再次显示开始位。 该图像应以一个像素的移动速度滚动,如果速度太慢,则应以更大的跳跃速度滚动。
我真的一直在寻找一些示例代码来展示如何执行此操作。
这不是我正在编写的游戏,所以不想使用 OpenGL 或任何花哨的东西。
到目前为止我看到的例子并不完全是我想要的。
我可以简化并准确说明我所坚持的是什么吗?
假设我将一张图像加载到内存中,该图像宽 1000 像素,高 100 像素。
有人可以向我展示几行代码,让我将一个矩形从 x 轴上的 200 像素开始剪切到这个更大的图像中,并且矩形本身是 100 x 100 像素。
我怎样才能切出这个矩形并将其显示在屏幕上 - 正如前面所说,速度并不重要。
然后我可以自己计算出滚动部分(我希望),并希望稍后将其发布回此处。
我正在按照 CGContext 类型命令的思路进行思考,但尽管查看了示例和文档,但我仍然遇到了困难。 我最绊倒的正是这种“切出矩形部分”。
谢谢。
任何帮助表示赞赏。
I am writing an ipad app that I have locked the orientation to landscape mode for.
I want to have a PNG file that is 100 pixels high but 4 times the width of the ipad screen.
What I want is this background image to be shown on the screen then automatically begin
scrolling on a timer. I have made sure the start and end parts of this image match up.
Once the end of the image is reached, I would like the start bit to show again.
This image should scroll along either at one pixel movements or larger jumps if that is too slow.
I am really stuck looking for some example code that would show how to show to do this.
It is NOT a game I am writing so do not want to use OpenGL or anything fancy.
The examples I have looked at so far are not exactly what I am after.
Can I simplify and state exactly what it is I am stuck on..
Lets say I load an image into memory that is 1000 pixels wide by 100 in height.
Can someone show me a few lines of code that would let me cut out a rectangle that begins say 200 pixels across the x axis into this larger image and is a rectangle itself 100 by 100 pixels.
How could I cut this rectangle out and show it on the screen - speed is not important as stated before.
I can then work out the scrolling part from this myself (I hope) and hopefully post it back here later..
I am thinking along the lines of the CGContext type commands but despite looking at examples and at docs am still up against a brick wall.
It is this 'cutting a rectangular section out' that I am stumbling most at.
Thanks.
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
披露 - 我根本不是 iPhone 开发者。
但是,我要做的就是将图像分成块。 (也许 8)所以,你最终会得到 8 个块,每个块高 100 像素,宽度为屏幕的 1/2。
然后,当图像滚动时,您可以在轮到显示之前加载每个图像。因此,当图像从屏幕上流出时,您可以将它们替换为要在屏幕另一侧显示的下一个图像。
Disclosure - I'm not an iPhone dev at all.
But, what I would do is break the image into chunks. (Maybe 8) So, you would end up having 8 chunks each 100px tall, and 1/2 the width of the screen.
Then, as the image scrolls, you can load each image before its turn to be displayed. So, as images flow off the screen, you can replace them with the next image to be displayed on the other side of the screen.
披露 - 我根本不是 iPhone 开发者。 (借用了 jjnguy)
获取背景图像并将其扩展一个屏幕的大小,然后将屏幕的开头复制到新区域。现在您所要做的就是裁剪所需的部分并显示它 - 环绕已经处理完毕。
Disclosure - I'm not an iPhone dev at all. (Borrowed that line from jjnguy)
Take your background image and expand it by one screen's worth, and copy the beginning of the screen to the new area. Now all you have to do is crop the desired part and display it - the wraparound has already been taken care of.
感谢您的所有帮助和建议。
最后,我决定最简单的方法就是马克·兰塞姆所建议的——因为这样可以解决问题。
我使用下面的 4 行代码剪出了我想要的 200 像素宽 x 100 像素高的部分。
然后我在计时器上循环这个变化的 startX 。完美的!
最后..它是 CGRectMake 和 CGImageCreateWithImageInRect 任何想要这样做的人都应该进一步研究!
谢谢大家。希望这对某人有帮助。
Thanks for all your help and advice.
In the end I decided the simplest way to do it was what Mark Ransom had suggested - as that takes care of the wraparound.
I used the 4 lines of code below to cut out a section I wanted 200 pixels wide by 100 high.
I then looped this varying startX on a timer. Perfect!
In the end.. it is CGRectMake and CGImageCreateWithImageInRect anyone wanting to do this should look further into!
Thanks all. Hope this helps someone.