2015-10-14 148 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中的功能相同,即: