UIBezierPath manipulation on iOS

心已入冬 提交于 2019-12-12 11:36:55

问题


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.

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.


回答1:


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);

}

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



来源:https://stackoverflow.com/questions/22720179/uibezierpath-manipulation-on-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!