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];}
- (void)drawingAtScreen{ UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTouch:)]; [self.drawView addGestureRecognizer:pan]; self.beizer = [UIBezierPath bezierPath]; [self initCAShaper];}
- (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; }}
[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;}
阅读全文
1 0
- iOS贝塞尔bezier曲线
- 绘制贝塞尔Bezier曲线
- 贝塞尔曲线(Bezier Curves)
- Bezier curve(贝塞尔曲线)
- cubic-bezier贝塞尔曲线函数
- Bezier曲线:
- bezier 曲线
- Bezier曲线
- Bezier曲线
- [贝塞尔曲线(Bezier Curves) 2d实现]
- UNITY贝塞尔曲线Bezier的一个细节
- cubic-bezier贝塞尔曲线CSS3动画工具
- cubic-bezier贝塞尔曲线CSS3动画工具
- cocos2d-x 贝塞尔曲线(Bezier)用法详解
- Android之贝塞尔曲线(Bezier)
- CSS3 三次贝塞尔曲线(cubic-bezier)
- 有理贝塞尔曲线(Rational Bezier Curves)
- 贝塞尔曲线——cubic-bezier详解
- Struts2-通配符
- Linux的df命令详解
- mysql数据库错误码大全
- Android hashmap 顺序取值
- 学习Javascript闭包(Closure)
- iOS贝塞尔bezier曲线
- 软件工程之软件开发方法、软件工具和开发环境
- MyBeatis实现数据库的增删改查(CRUD)
- POJ 3279
- C.Primer.Plus(第六版)第17章 编程练习
- Android AIDL 使用详解
- 实现一个列表只需一行代码-RecyclerView适配器暴力封装
- 水经微图功能简介
- kylin使用中曾遇到的问题整理