辞客堂数位杂谈版 2018-04-19
一个曲线
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]; }
图例: