View of audio and wave graphics when playing music
This effect is similar to this:
The effect is as follows:
Source code:
Musicview. h and musicview. m
//// Musicview. h // music // copyright (c) 2014 y. x. all rights reserved. // # import <uikit/uikit. h> @ interface musicview: uiview @ property (nonatomic, assign) cgfloat progress; // process percentage. Value: [0, 1] @ property (nonatomic, assign) cgfloat timeinterval; // time interval @ end
//// Musicview. M // music // copyright (c) 2014 y. x. all rights reserved. // # import "musicview. H "@ interface musicview () @ property (nonatomic, assign) cgrect baserect; // back up the original frame value @ end @ implementation musicview-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {_ baserect = frame;} return self ;}@ synthesize progress = _ progress;-(void) setprogress :( cgfloat) Progress {if (Progress <= 0) {_ progress = 0;} else if (Progress >=1) {_ progress = 1 ;}else {_ progress = progress ;} [uiview animatewithduration :( _ timeinterval> 0? _ Timeinterval: 0.99) animations: ^ {cgrect rect = _ baserect; rect. size. height * = _ progress; self. frame = rect ;}] ;}- (cgfloat) SS {return _ progress ;}@ end
Usage:
//// Rootviewcontroller. M // music // copyright (c) 2014 y. x. all rights reserved. // # import "rootviewcontroller. H "# import" musicview. H "@ interface rootviewcontroller () @ property (nonatomic, strong) nstimer * timer; @ property (nonatomic, strong) musicview * musicviewline1; @ property (nonatomic, strong) musicview * musicviewline2; @ property (nonatomic, strong) musicview * musicviewline3; @ end @ implementation rootviewcontroller-(void) viewdidload {[Super viewdidload]; // line 1 _ musicviewline1 = [[musicview alloc] initwithframe: cgrectmake (100,100, 4, 20)]; _ blank = [uicolor redcolor]; _ musicviewline1.timeinterval = 0.5f; [self. view addsubview: _ musicviewline1]; // line 2 _ musicviewline2 = [[musicview alloc] initwithframe: cgrectmake (108,100, 4, 20)]; _ musicviewline2.backgroundcolor = [uicolor redcolor]; _ musicviewline2.timeinterval = 0.5f; [self. view addsubview: _ musicviewline2]; // line 3 _ musicviewline3 = [[musicview alloc] initwithframe: cgrectmake (116,100, 4, 20)]; _ musicviewline3.backgroundcolor = [uicolor redcolor]; _ musicviewline3.timeinterval = 0.5f; [self. view addsubview: _ musicviewline3]; _ timer = [nstimer scheduledtimerwithtimeinterval: 0.5f target: Self selector: @ selector (timerevent) userinfo: Nil repeats: Yes];}-(void) timerevent {_ musicviewline1.progress = arc4random () % 100/100. f; _ musicviewline2.progress = arc4random () % 100/100. f; _ musicviewline3.progress = arc4random () % 100/100. f ;}@ end
The following is the core code: