iOS小demo之二阶贝塞尔画线

本文介绍了一个使用二阶贝塞尔曲线在iOS中画线的方法。通过定义起始点、结束点和控制点,实现了平滑的线条绘制。代码中包括计算中点的函数、设置上下文属性以及使用`CGContextAddQuadCurveToPoint`进行曲线绘制的步骤。
摘要由CSDN通过智能技术生成

CGPoint midPoint(CGPoint p1, CGPoint p2)

{

    return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);

    

}

  


  CGContextRef contextRef;

    

    CGPoint point1;//起始点

    

    CGPoint point2;//结束点

    

    CGPoint currentPoint;//当前点



- (void)DrawWithPoint:(CGPoint)point{

    

    point1 = point2;//起点

    

    point2 = currentPoint;//终点

    

    currentPoint = point;//当前点

    

    CGPoint mid1 = midPoint(point2, point1);//起点控制点

    

    CGPoint mid2 = midPoint(point, point2);//终点控制点

    

    contextRef = UIGraphicsGetCurrentContext();


    CGContextSetAllowsAntialiasing(contextRef, YES);

    

    CGContextSetShouldAntialias(contextRef, YES);

    

    CGContextSetLineCap(contextRef, kCGLineCapRound);

    

    CGContextBeginPath(contextRef);

    

    CGContextSetLineWidth(contextRef, _linewidth);//绘制宽度

    

    CGContextSetStrokeColorWithColor( contextRef, _linecolor.CGColor);//绘制颜色

    

    CGContextSetShadowWithColor(contextRef, CGSizeMake(0.5, 0.5), 0.5f,_linecolor.CGColor);//阴影效果

    

    CGContextMoveToPoint(contextRef, mid1.x, mid1.y);//绘制起点

    

    CGContextAddQuadCurveToPoint(contextRef, point2.x, point2.y, mid2.x, mid2.y);//绘制终点

    

    CGContextStrokePath(contextRef);

    

    drawImage = UIGraphicsGetImageFromCurrentImageContext();

    

    drawImageView.image = drawImage;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值