iOS 上的 UIBezierPath 操作 [英] UIBezierPath manipulation on iOS

查看:18
本文介绍了iOS 上的 UIBezierPath 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一条直线开始,我希望用户能够触摸和拖动这条线,使其弯曲.实际上,他们将有能力将线操纵成波浪形.我不确定在技术上实现这一目标的最简单方法.

I'm starting out with a straight line and I would like the user to be able to touch and drag the line so that it curves. Effectively, they would have the ability to manipulate the line into a wave shape. I'm not sure of the easiest way of achieving this technically.

我首先创建了一个三次曲线的 UIBezierPaths 数组,目的是操纵控制点 - 但似乎一旦绘制 UIBezierPaths 的控制点就不可能改变它们?

I had started by creating an array of UIBezierPaths of cubic curves with the intention of manipulating the control points - but it appears that it is not possible to alter the control points of UIBezierPaths once they have been drawn?

对于最简单的技术方法的任何建议表示赞赏.

Any suggestions as to the simplest technical approach appreciated.

推荐答案

可以在drawRect中使用变量作为控制点绘制quadCurve(一个控制点贝塞尔曲线),然后在touchesBegan方法中更新控制点如下.注意需要用setNeedsDisplay重绘

You can draw a quadCurve (one control point Bezier curve) in drawRect using variables as control points, and then update the control points in the touchesBegan method as follows. Note that you need to redraw with setNeedsDisplay

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    controlPoint = [[touches anyObject] locationInView:self];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetLineWidth(context, 1.0);

    CGContextMoveToPoint(context, 0, 320);
    CGContextAddQuadCurveToPoint(context, controlPoint.x, controlPoint.y, 320, 200);
    CGContextStrokePath(context);

}

如果你有多个控制点,那么touchesMoved方法中需要一些视觉元素和额外的逻辑... e

If you have multiple control points, then some visual elements and extra logic is required in the touchesMoved method... e

这篇关于iOS 上的 UIBezierPath 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆