iOS绘图 - 基本线条的绘制

LineView.h

#import <UIKit/UIKit.h>

@interface LineView : UIView

@end

LineView.m

#import "LineView.h"

@implementation LineView

- (void)drawRect:(CGRect)rect {

    [self draw2CurveLine];   
}

/** * 画直线 */
- (void)drawStraightLine
{
    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = self.frame.size.height;

    //获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //线条一
    UIBezierPath *path1 = [[UIBezierPath alloc] init];
    [path1 moveToPoint:CGPointMake(0, 0)];
    [path1 addLineToPoint:CGPointMake(viewW, viewH)];

    //线条二
    UIBezierPath *path2 = [[UIBezierPath alloc] init];
    [path2 moveToPoint:CGPointMake(viewW, 0)];
    [path2 addLineToPoint:CGPointMake(0, viewH)];

    CGContextAddPath(ctx, path1.CGPath);
    CGContextAddPath(ctx, path2.CGPath);

    //线条粗细
    CGContextSetLineWidth(ctx, 2);
    //线条头尾样式
    CGContextSetLineCap(ctx, kCGLineCapButt);
    //线条颜色
    CGContextSetRGBStrokeColor(ctx, 250/255.0, 208/255.0, 50/255.0, 1);
    //线条转折点样式
    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    CGContextStrokePath(ctx);
}

/** * 画折线 */
- (void)drawTurningLine
{
    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = self.frame.size.height;

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    UIBezierPath *path = [[UIBezierPath alloc] init];

    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(viewW * 0.5, viewH * 0.5)];
    [path addLineToPoint:CGPointMake(viewW, 0)];

    // Set the color: Sets the fill and stroke colors in the current drawing context.
    [[UIColor yellowColor] set];

    //Set the line width in the current graphics
    CGContextSetLineWidth(ctx, 4);


    CGContextSetLineJoin(ctx, kCGLineJoinRound);

    CGContextAddPath(ctx, path.CGPath);

    CGContextStrokePath(ctx);
}

/** * 画曲线 */
- (void)drawCurveLine
{
    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = self.frame.size.height;

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGPoint startP = CGPointMake(0,viewH * 0.3);
    CGPoint endP = CGPointMake(viewW, viewH * 0.3);
    CGPoint controlP = CGPointMake(viewW * 0.5, viewH * 0.6);

    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:startP];
    [path addQuadCurveToPoint:endP controlPoint:controlP];

    CGContextAddPath(ctx, path.CGPath);

    CGContextStrokePath(ctx);
}

/** * 画双曲线 */
- (void)draw2CurveLine {

    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = self.frame.size.height;

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    UIBezierPath *path = [[UIBezierPath alloc] init];

    [path moveToPoint:CGPointMake(0, 0)];
    [path addCurveToPoint:CGPointMake(viewW, viewH) controlPoint1:CGPointMake(0, viewH * 0.5) controlPoint2:CGPointMake(viewW, viewH * 0.5)];

    CGContextAddPath(ctx, path.CGPath);
    CGContextStrokePath(ctx);
}

@end

你可能感兴趣的:(绘图,贝塞尔曲线)