作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试以这种方式手动创建贝塞尔曲线:
- (void)drawBezierFrom:(CGPoint)from to:(CGPoint)to controlA:(CGPoint)a controlB:(CGPoint)b sections:(NSUInteger)cnt color:(NSUInteger)color
{
float qx, qy;
float q1, q2, q3, q4;
int lastx = - 1, lasty;
int plotx, ploty;
float t = 0.0;
while (t <= 1)
{
q1 = t*t*t*-1 + t*t*3 + t*-3 + 1;
q2 = t*t*t*3 + t*t*-6 + t*3;
q3 = t*t*t*-3 + t*t*3;
q4 = t*t*t;
qx = q1*from.x + q2*a.x + q3*to.x + q4*b.x;
qy = q1*from.y + q2*a.y + q3*to.y + q4*b.y;
plotx = round(qx);
ploty = round(qy);
/*if (lastx != -1)
[self drawLineFrom:NSMakePoint(lastx, lasty) to:NSMakePoint(plotx, ploty) color:color];
else
[self drawLineFrom:NSMakePoint(from.x, from.y) to:NSMakePoint(plotx, ploty) color:color];*/
lastx = plotx;
lasty = ploty;
t = t + (1.0/(cnt + 0.0f));
}
//[self drawLineFrom:NSMakePoint(lastx, lasty) to:NSMakePoint(to.x, to.y) color:color];
[self drawLineFromCoordX:lastx andY:lasty toPointWithCoordsX:to.x andY:to.y];
}
-(void)drawLineFromCoordX:(int)x andY:(int)y toPointWithCoordsX: (int)fx andY: (int)fy{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, x,y);
CGContextAddLineToPoint(context, fx,fy);
CGContextStrokePath(context);
}
调用viewDidLoad中的方法:
CGPoint fromPoint = CGPointMake(10, 10);
CGPoint toPoint = CGPointMake(100, 10);
CGPoint controlA = CGPointMake(50, 50);
CGPoint controlB = CGPointMake(60, 60);
[self drawBezierFrom:fromPoint to:toPoint controlA:controlA controlB:controlB sections:10 color:4];
编辑:
我做了你所做的事情:
-(void)drawRect:(CGRect)rect{
CGPoint fromPoint = CGPointMake(10, 10);
CGPoint toPoint = CGPointMake(100, 10);
CGPoint controlA = CGPointMake(50, 50);
CGPoint controlB = CGPointMake(60, 60);
[self drawBezierFrom:fromPoint to:toPoint controlA:controlA controlB:controlB sections:10 color:4];
}
保持其他一切不变,但它仍然不起作用
最佳答案
将你在 viewDidLoad 中的代码([super viewDidLoad] 除外)放入 drawRect 方法中。需要在 drawRect 方法中调用 UIGraphicsGetCurrentContext() 才能知道当前上下文是哪个。
编辑:
还放:
[self drawLineFromCoordX:lastx andY:lasty toPointWithCoordsX:to.x andY:to.y];
在 while 循环中。在 while 循环之前设置描边颜色:
[[UIColor redColor] setStroke];
关于ios - 手动绘制贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658983/
我是一名优秀的程序员,十分优秀!