天天看點

給定一組點畫三次貝塞爾曲線

關于過已知點畫平滑曲線,這裡有一篇比較好了解的部落格http://blog.csdn.net/microchenhong/article/details/6316332

需求是:給定一組資料,将其用平滑曲線描繪出來,畫成一個曲線統計圖。

我這裡直接将資料根據公司需求換成了一個存放“CGPoint”的數組,直接上代碼:

//根據points中的點畫出曲線
- (void)drawCurveChartWithPoints:(NSMutableArray *)points
{
    UIBezierPath* path1 = [UIBezierPath bezierPath];
    [path1 setLineWidth:];     //設定畫筆寬度
    //周遊各個點,畫曲線
    for(int i=; i<[points count]-; i++){
        CGPoint startPoint = [[points objectAtIndex:i] CGPointValue];
        CGPoint endPoint = [[points objectAtIndex:i+] CGPointValue];
        [path1 moveToPoint:startPoint];
        [UIView animateWithDuration: animations:^(){
            [path1 addCurveToPoint:endPoint controlPoint1:CGPointMake((endPoint.x-startPoint.x)/+startPoint.x, startPoint.y) controlPoint2:CGPointMake((endPoint.x-startPoint.x)/+startPoint.x, endPoint.y)];
        }];
    }
    [self.lineColor set];       //設定畫筆顔色
    path1.lineCapStyle = kCGLineCapRound;
    [path1 strokeWithBlendMode:kCGBlendModeNormal alpha:];
}
           

PS:這是我暫時找到的較好的方法了,如果有更Nice的方法,請給我留言。不勝感激。。。

繼續閱讀