Reputation: 1260
I need to make a project where I will draw some graphics on the screen, its about 25 separated bars, I've done it with UIView, but unfortunately not all devices can handle this job, because there is a 25x25 matrix of squares of UIViews, they are updating its color and alpha in 0.04 seconds, and its really takes a lot of memory to draw it like this, can someone please help with a purpose, how can it all be done faster for memory managing, and if its possible to control its components like alpha or background color for an object. Thanks in advance
Upvotes: 0
Views: 164
Reputation: 27506
What I understand is that you use 25 subviews for each bar. You can already optimize that and make each bar a single UIView
. You can make a custom subclass of UIView
that we will call BarView
and override its drawRect
method to draw the 25 squares of the bar.
Edit
Assuming you have an array bars
that contains 25 bars (of type Bar
). Each bar contains an array squares
of 25 squares (of type Square
).
- (void)drawRect:(CGRect)rect
{
CGContextRef *ctx = UIGraphicsGetCurrentContext();
for (Bar *bar in self.bars) {
for (Square *square in bar.squares) {
CGContextSetFillColorWithColor(ctx, square.color.CGColor);
CGContextFillRect(ctx, square.frame);
}
}
}
Bar
and Square
do not subclass UIView
, they are simple NSObject
s.
Finally note that this code is only an example and that there are possibilities to further improve performance.
Upvotes: 3
Reputation: 81848
You might want to switch to a Core Animation based drawing approach as proposed in this answer. The idea is to draw as few things as possible using Core Graphics. Instead you create layers for all your independently animating graphical elements and animate layer properties.
To draw bars you could create a layer and set its backgroundColor and bounds/center properties to animate its size and appearance.
Upvotes: 1