接上文。这是当音频输入后用一个表示音频输入的大小变化的view
这个没有什么可以说的,直接上代码
#import@interface AVMeterView : UIView { float peakPowerForChannel; float h;}@property(nonatomic,assign) float peakPowerForChannel;- (id)initWithFrame:(CGRect)frame;@end
#import "AVMeterView.h"#import#import @implementation AVMeterView@synthesize peakPowerForChannel;- (void)dealloc { [ super dealloc ];}- (id)initWithFrame:(CGRect)frame { self = [ super initWithFrame: frame ]; if (self != nil) { self.backgroundColor = [UIColor clearColor]; h = 100; self.layer.cornerRadius = 8; self.layer.masksToBounds = YES; } return self;}- (void)drawRect:(CGRect)rect { CGRect viewBounds = self.bounds; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClearRect(context, viewBounds); CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5].CGColor)); CGContextFillRect(context, viewBounds); CGContextSetLineWidth(context,5); CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); if (peakPowerForChannel > 50) { peakPowerForChannel = 50; } for (int i= 20; i <= peakPowerForChannel * 1.2; i = i+ 10) { float hs = sqrtf(2) * i; float p = h/sqrtf(2); float p1 = (hs)/2; CGContextMoveToPoint(context,p - p1,h - p1); CGContextAddArcToPoint(context,p,h - hs,p + p1,h - p1,i); } CGContextStrokePath(context); // add image CGContextTranslateCTM(context, 0, viewBounds.size.height); CGContextScaleCTM(context, 1, -1); UIImage *image = [UIImage imageNamed:@"mike.png"]; CGFloat x = self.bounds.size.width - image.size.width; CGContextDrawImage(context, CGRectMake(x*0.5,10, 34, 44), image.CGImage);}@end
出来的图片就是中间那部分。其实啪啪和微信都差不多是这样子做的,很简单的。