0

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

Florik
  • 1,260
  • 1
  • 12
  • 20
  • Alpha: https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/alpha BGcolor: https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/backgroundColor – Marco Pace Mar 05 '12 at 11:53

3 Answers3

3

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 NSObjects.

Finally note that this code is only an example and that there are possibilities to further improve performance.

sch
  • 27,436
  • 3
  • 68
  • 83
  • I've done it with UIView, my bar it is inherited from UIView. – Florik Mar 05 '12 at 12:29
  • 1
    Yes, and does it contain subviews? If yes, then you should remove them and use `drawrect` instead to draw the squares of the bar. If you need more help, you should explain how the bars look and what functionalities they have. – sch Mar 05 '12 at 12:59
  • the bars are drawn on one subview, i separate them by tags, and by for cicle i set the color and alpha ... the matrix is 25x25, there are 625 subviews on a view ... like this .. – Florik Mar 05 '12 at 13:07
1

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.

Community
  • 1
  • 1
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • thank you, already tried to make it with CALayers, but unfortunately it works worse ... the bars are animating worser than they should !! – Florik Mar 05 '12 at 12:27
1

Go for OpenGL. It has very efficient 2D drawing mechanism as well.

MrTJ
  • 13,064
  • 4
  • 41
  • 63