一個曲線 圖例: 多個曲線 圖例: ...
一個曲線
UIColor *color = [UIColor redColor]; [color set]; UIBezierPath *path = [UIBezierPath bezierPath]; ///線的寬度【粗細】 path.lineWidth = 5; ///端點類型 path.lineCapStyle = kCGLineCapRound; ///銜接類型 path.lineJoinStyle = kCGLineJoinRound; ///起點 [path moveToPoint:CGPointMake(0, 100)]; [path addCurveToPoint:CGPointMake(200, 100) controlPoint1:CGPointMake(50, 0) controlPoint2:CGPointMake(150, 200)]; [path stroke];
圖例:
多個曲線
/** 貝塞爾曲線 CGRect */ - (void)draweBezierCurves:(CGRect)rect{ ///曲線水平位置y坐標 CGFloat lintY = rect.origin.y; ///每個曲線的水平長度【兩個弧度為一個曲線】 NSInteger lineWidth = 8; ///每個曲線的上下頂點與水平位置 距離 CGFloat lingPeak = 3; ///波浪線個數 NSInteger lineCount = (int)(rect.size.width / lineWidth); ///除了整數外,最後一條線的長度【小於 lineWidth】 CGFloat lastLineWidth = rect.size.width - lineCount*lineWidth; ///貝塞爾 UIBezierPath *path = [UIBezierPath bezierPath]; ///線的寬度【粗細】 path.lineWidth = 1; ///端點類型 path.lineCapStyle = kCGLineCapRound; ///銜接類型 path.lineJoinStyle = kCGLineJoinRound; ///顏色 UIColor *color = [UIColor redColor]; [color set]; for (int i = 0; i < lineCount; i ++) { ///起點 [path moveToPoint:CGPointMake(rect.origin.x + i*lineWidth, lintY)]; ///三點成線 [path addCurveToPoint:CGPointMake(rect.origin.x + i*lineWidth + lineWidth, lintY) controlPoint1:CGPointMake(rect.origin.x + i*lineWidth + lineWidth/4, lintY - lingPeak) controlPoint2:CGPointMake(rect.origin.x + i*lineWidth + (lineWidth/4)*3, lintY + lingPeak)]; } if (lastLineWidth > 0) { ///起點 [path moveToPoint:CGPointMake(rect.origin.x + lineCount*lineWidth, lintY)]; ///三點成線 [path addCurveToPoint:CGPointMake(rect.origin.x + lineCount*lineWidth + lastLineWidth, lintY) controlPoint1:CGPointMake(rect.origin.x + lineCount*lineWidth + lastLineWidth/4, lintY - lingPeak) controlPoint2:CGPointMake(rect.origin.x + lineCount*lineWidth + (lastLineWidth/4)*3, lintY + lingPeak)]; } [path stroke]; }
圖例: