2015-10-14 71 views
2

我正在使用Superpowered SDK来播放声音。 它有一个函数返回名为peakWaveForm的无符号字符**。 我写了一个自定义的uiview,并试图绘制这个值,我的视图没有很好看。我的问题是,如何绘制我的波形值? 和什么样的变量。数组?。波形的正常大小应该是多少? SDK返回一个无符号字符**我该如何继续?如何绘制波形?

- (void)drawRect:(CGRect)updateRect 
{ 
    unsigned i, maxIndex; 

    maxIndex = floor(CGRectGetMaxX(updateRect)); 
    i = floor(CGRectGetMinX(updateRect)); 
    float firstPoint = (float)mPeakWaveForm[0][i]; 

    UIBezierPath *path = [UIBezierPath bezierPath]; 
    path.lineWidth = 2; 
    [[UIColor blackColor] setFill]; 
    [path moveToPoint:CGPointMake(i,firstPoint)]; 

    for(i; i <= maxIndex; i++) 
    { 
     float nextPoint = (float)mPeakWaveForm[0][i]; 
     [path addLineToPoint:CGPointMake(i, nextPoint)]; 
    } 
    [path fill]; 
} 

回答

0

我和你处于同样的境地,并且用这种方式解决了这个问题。 首先,您只能获取一维数据数组中的波形,并且我们希望它在两个轴中都有它。 所以我所做的是建立一个数组点,而不是直接绘制路径,然后一次绘制路径的和另一个时间镜像角落找寻x轴为这样:

var points = Array<CGPoint>() 
    for(i; i <= maxIndex; i++) 
    { 
     float nextPoint = (float)mPeakWaveForm[0][i]; 
     points.append(CGPoint(x: CGFloat(i), y: CGFloat(nextPoint))) 
    } 
    //Move to the center of the view 
    var xf = CGAffineTransformIdentity; 
    xf = CGAffineTransformTranslate(xf, 0, halfHeight) 
    //Scale it as needed (you can avoid it) 
    xf = CGAffineTransformScale(xf, xscale, yscale) 

    let path = CGPathCreateMutable() 
    //Draw the lines 
    CGPathAddLines(path, &xf, points, points.count) 

    //Mirror the drawing and draw them again 
    xf = CGAffineTransformScale(xf, 1.0, -1.0); 
    CGPathAddLines(path, &xf, points, points.count); 

    CGPathCloseSubpath(path) 
    //Draw the path 
    let ctx = UIGraphicsGetCurrentContext(); 
    CGContextAddPath(ctx, path); 
    CGContextSetStrokeColorWithColor(ctx,UIColor.whiteColor().CGColor); 
    CGContextFillPath(ctx); 

很抱歉的代码迅速之中,但功能与Objective-C中的功能相同,即: