4

I want to make a visualisation for my music player.so that i draw a grid view and i want to change each square colour randomly or continuously.

My Code for draw grid

- (void)drawRect:(CGRect)rect

 for (int i = 0; i < 4 ;i = i + 1) {
    for (int j = 0; j < 4; j = j + 1) {
        CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 2.0);

CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

CGRect rectangle = CGRectMake((j*(100+2))+2,(i*(100+2))+2,100,100);

CGContextAddRect(context, rectangle);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
        CGContextFillPath(context);
CGContextStrokePath(context);
    }
}
}

it look like this image

iamVishal16
  • 1,780
  • 18
  • 40
  • what exactly are you asking for? do you want a function to create random colors or do you want to know how you can trigger your drawRect method continuously? – Argent May 14 '14 at 07:41
  • 1
    How i trigger draw rect method according to music beats and i also want to changes on square colour randomly and continuously.Can u please help me ? – iamVishal16 May 14 '14 at 09:57

2 Answers2

1

In my opinion you are overcomplicating yourself, and limiting future possibilities. If i were you, i would have a grid of UIViews or UIImageViews placed in an array. (You can do it programmatically or with the IB). (You can add the edges by modifying the border property in the view layer)

Then you can do all sort of things by setting their background colors independently, color evens, color odds, random all, anything you want since all you have to do is cycle through the array setting the colors accordingly per beat.

For the beats part is way more complicated than it seems. check this question, it offers a lot of tips on "music information retrieval".

How to detect the BPM of a song in php

Community
  • 1
  • 1
Pochi
  • 13,391
  • 3
  • 64
  • 104
  • Yes you are right but i want beats from IPOD native class MPMusicPlayerController so how i can retrieve using this class. Can u please explain ? – iamVishal16 May 16 '14 at 06:09
  • Do you mean the beats per minute? or you want some kind of method to be called for every beat of the song? look at this question http://stackoverflow.com/questions/5250918/access-bpm-field-on-a-song-mpmediaitempropertybeatsperminute-not-working – Pochi May 16 '14 at 06:22
  • this link can't help me ?? – iamVishal16 May 22 '14 at 07:49
  • 1
    The main issue is that you are asking two completely different questions, one is related to visualization, while the other talks about sound analysis. – Pochi May 23 '14 at 01:37
  • Also the link I posted talks about the analysis part, If you want more help you have to be SUPER specific on what you want to achieve. – Pochi May 23 '14 at 02:02
-3

I think you should have a single custom UIView.

Then call at short intervals setNeedsDisplayInRect: with the area of the view that you want to redraw.

Finally implement drawRect: and make sure to make optimize it by only redrawing the specified area, and doing it fast!

As for the music beats, better open a separate question ;)

Rivera
  • 10,792
  • 3
  • 58
  • 102
  • Any feedback on the down votes? Changing the background color of multiple views requires multiple calls to `drawRect:` on multiple views (handled by the default `UIView` implementation). My approach would call multiple `drawReact:` on a single view which I think would be good for performance besides having more freedom to draw anything. – Rivera May 22 '14 at 08:07