Reputation: 13364
I'm making an iPhone game and using UIView
objects to draw sprites. Most of the time, I have no performance problems. However, once I have around 15 to 20 objects on the screen (and maybe 5 of them moving around), the game becomes considerably slower, especially on the iPhone 3G. The frame rate can drop to as low as a single frame per second.
Is this simply a limitation of using UIView
objects, or should iOS be able to handle this many UIView
objects on screen at the same time?
In order to isolate the problem, I've made drawing my views very simple — drawing a red rectangle. drawRect
is only getting called once per view. The view hierarchy is very simple and shallow. I'm using CADisplayLink
to update the UIView
locations every frame.
There's very little else going on, so I'd like to hear if anyone else has had success using this number of UIView
objects.
Upvotes: 2
Views: 1721
Reputation: 35394
If you're setting the opaque
property of each view to NO
, keep in mind that this seriously affects the speed of drawing the views. If your views aren't transparent, you should leave this set to YES
, which is default.
Upvotes: 3
Reputation: 13364
The key to my problems ended up being that I had labels on top of my game content. The labels are not opaque, which likely was a large part of the problem, as phix23 suggested.
The first thing that made a big difference was removing a frames per second label that was on top of the content. Having a label that changed content on every frame caused a lot of slowdown.
I also had a large label that displayed on top of much of the game and changed shape when you level up. It turned out that drawing this label on top of everything caused a lot of slowdown as well.
In answer to my original question, I've found that on an iPhone 3G I can support about 30-40 opaque UIViews onscreen at the same time, with 2 or 3 non-opaque views as well. Non-opaque UIViews that change size, shape, or location are by far the worst, and even one of these that covers a significant amount of the screen can quickly cause problems.
Upvotes: 3
Reputation:
for such type of application you should use CoreGraphics / Quartz / OpenGL but anyway I don't think there is a limitation on such low count. For example if I have a table view with 9 rows and each row has 5 subviews its still displayed acceptable fast. Have you tried using UIView animation to change the position in view?
good luck in learning OpenGL ;)
Upvotes: 0