iOS贝塞尔bezier曲线

来源:互联网 发布:数据整理分析报告模板 编辑:程序博客网 时间:2024/06/05 20:11

一.常规使用

属性:

@property(nonatomic,strong) CAShapeLayer *shapeLayer;@property(nonatomic,strong) UIBezierPath *beizer;@property(nonatomic,assign) CGPoint startPoint;@property(nonatomic,assign) CGPoint movePoint;@property(nonatomic,strong) UIView *drawView;

方法:

1.初始化CAShapeLayer:

- (void)initCAShaper{    self.shapeLayer = [[CAShapeLayer alloc] init];    self.shapeLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);    self.shapeLayer.fillColor = nil;    self.shapeLayer.lineCap = kCALineCapRound;    self.shapeLayer.strokeColor = [UIColor whiteColor].CGColor;    self.shapeLayer.lineWidth = 10;    [self.drawView.layer addSublayer:self.shapeLayer];}


2.初始化触控手势、画图区域view、贝塞尔路径,并执行初始化方法 1

- (void)drawingAtScreen{    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTouch:)];    [self.drawView addGestureRecognizer:pan];    self.beizer = [UIBezierPath bezierPath];    [self initCAShaper];}


3.触控手势响应,处理触控坐标点,生成贝塞尔路径并赋值给shapeLayer

- (void)panTouch:(UIPanGestureRecognizer *)panGesture{    _startPoint = [panGesture locationInView:self.drawView];    if (panGesture.state == UIGestureRecognizerStateBegan) {        [self.beizer moveToPoint:_startPoint];    }    if (panGesture.state == UIGestureRecognizerStateChanged) {        _movePoint = [panGesture locationInView:self.drawView];                [_beizer addLineToPoint:_movePoint];        self.shapeLayer.path = _beizer.CGPath;    }}


4.清空图像

        [self.beizer removeAllPoints];        self.shapeLayer.path = _beizer.CGPath;



二.绘制正弦曲线

属性:

@property(nonatomic,strong) CAShapeLayer *shapeLayer;@property(nonatomic,strong) UIBezierPath *beizer;@property(nonatomic,assign) CGPoint startPoint;@property(nonatomic,strong) UIView *drawView;


方法:

1.初始化CAShapeLayer:

//初始化图层- (void)initCAShaper{    self.beizer = [UIBezierPath bezierPath];        self.drawView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];    self.drawView.backgroundColor = [UIColor blackColor];    [self.view addSubview:self.drawView];        self.shapeLayer = [[CAShapeLayer alloc] init];    self.shapeLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);    self.shapeLayer.fillColor = nil;    self.shapeLayer.lineCap = kCALineCapRound;    self.shapeLayer.strokeColor = [UIColor blueColor].CGColor;    self.shapeLayer.lineWidth = 3;    [self.drawView.layer addSublayer:self.shapeLayer];}

2.获取贝塞尔控制点

- (NSArray *)getControlPointWithPoint0:(CGPoint)point0 andPoint1:(CGPoint)point1 andPoint2:(CGPoint)point2{     //   CGFloat smooth_value = 0.6;    CGPoint mediumC1 = CGPointMake((point0.x+point1.x)/2, (point0.y+point1.y)/2);    CGPoint mediumC2 = CGPointMake((point1.x+point2.x)/2, (point1.y+point2.y)/2);        CGFloat len0_1 = sqrt((point0.x-point1.x)*(point0.x-point1.x) + (point0.y-point1.y)*(point0.y-point1.y));    CGFloat len1_2 = sqrt((point1.x-point2.x)*(point1.x-point2.x) + (point1.y-point2.y)*(point1.y-point2.y));        CGFloat k = len0_1/len1_2;    CGFloat xk = (k*mediumC2.x+mediumC1.x)/(k+1);    CGFloat yk = (k*mediumC2.y+mediumC1.y)/(k+1);    CGPoint K = CGPointMake(xk, yk);    CGFloat diffX = point1.x - K.x;    CGFloat diffY = point1.y - K.y;        CGPoint ctl1 = CGPointMake(mediumC1.x+diffX, mediumC1.y+diffY);    CGPoint ctl2 = CGPointMake(mediumC2.x+diffX, mediumC2.y+diffY);        NSValue *ctlpoint1 = [NSValue valueWithCGPoint:ctl1];    NSValue *ctlpoint2 = [NSValue valueWithCGPoint:ctl2];        NSArray *pointsArray = [NSArray arrayWithObjects:ctlpoint1, ctlpoint2, nil];        return pointsArray;}

3.绘制正弦曲线

- (void)drawPathTest1{    CGFloat mm = 300;        NSMutableArray *pointArray = [[NSMutableArray alloc] init];        CGFloat sinf = 3.1415926*2/100;        for(NSInteger cc=0;cc<100;cc++){        CGFloat sinY = sin(sinf*cc);        CGFloat sinyy = sinY*100 + 300;        CGPoint ppp = CGPointMake(cc*2, sinyy);        NSValue *pppv = [NSValue valueWithCGPoint:ppp];        [pointArray addObject:pppv];    }            [self.beizer moveToPoint:CGPointMake(0, 300)];        for(NSInteger i=0;i<pointArray.count-2;i++){        NSValue *vpoint1 = [pointArray objectAtIndex:i];        NSValue *vpoint2 = [pointArray objectAtIndex:i+1];        NSValue *vpoint3 = [pointArray objectAtIndex:i+2];        CGPoint pp1 = vpoint1.CGPointValue;        CGPoint pp2 = vpoint2.CGPointValue;        CGPoint pp3 = vpoint3.CGPointValue;                NSArray *ppp = [self getControlPointWithPoint0:pp1 andPoint1:pp2 andPoint2:pp3];        NSValue *ctlValue1 = [ppp objectAtIndex:0];        NSValue *ctlValue2 = [ppp objectAtIndex:1];        CGPoint ctlpoint1 = ctlValue1.CGPointValue;        CGPoint ctlpoint2 = ctlValue2.CGPointValue;                [self.beizer addCurveToPoint:pp3 controlPoint1:ctlpoint1 controlPoint2:ctlpoint2];    }        self.shapeLayer.path = self.beizer.CGPath;//    [self.beizer moveToPoint:p1];//    [self.beizer addLineToPoint:p2];//    self.shapeLayer.path = self.beizer.CGPath;}












原创粉丝点击