我有以下代码,允许绘制曲线。用户绘制直线并且一旦触摸结束,控制手柄就会出现,用户可以将线修改为所需的曲线。它适用于所有iPad上的IOS 6和IOS 7。但它在IOS 8上运行的iPad Air和iPad mini 2上运行起来很奇怪。当用户向一个方向拉动手柄时,起始点移动到相反的位置。所以它就像反向坐标计算。
-(void)drawStraightLine
{
CGContextRef context = UIGraphicsGetCurrentContext();
// set the line properties
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 2.5);
CGContextSetAlpha(context, 1.0);
// draw the line
CGContextMoveToPoint(context, d.x, d.y);
CGContextAddLineToPoint(context, c.x, c.y);
CGContextStrokePath(context);
}
-(void)showHandles
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor grayColor].CGColor);
CGContextMoveToPoint(ctx, a.x, a.y);
CGContextAddLineToPoint(ctx, b.x, b.y);
CGContextMoveToPoint(ctx, c.x, c.y);
CGContextAddLineToPoint(ctx, d.x, d.y);
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx, a.x, a.y);
for (double t = 0; t < 1; t += EPSILON) {
CGPoint p = [self bezier:t];
CGContextAddLineToPoint(ctx, p.x, p.y);
}
CGContextSetStrokeColorWithColor(ctx, self.lineColor.CGColor);
CGContextSetLineWidth(ctx, 2.5);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
CGContextStrokePath(ctx);
//[self showLines:ctx];
CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
CGContextFillEllipseInRect(ctx, CGRectMake(a.x-5, a.y-5, 10, 10));
CGContextFillEllipseInRect(ctx, CGRectMake(b.x-5, b.y-5, 10, 10));
CGContextFillEllipseInRect(ctx, CGRectMake(c.x-5, c.y-5, 10, 10));
CGContextFillEllipseInRect(ctx, CGRectMake(d.x-5, d.y-5, 10, 10));
CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
CGContextSelectFont(ctx, "Helvetica", 18.0, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(ctx, kCGTextFill);
CGContextSetTextMatrix(ctx, CGAffineTransformMakeScale(1.0, -1.0));
NSString *label = [NSString stringWithFormat:@"(%.0f,%.0f)", a.x, a.y];
label = [NSString stringWithFormat:@"(%.0f,%.0f)", c.x, c.y];
CGContextShowTextAtPoint(ctx, fmax(30, fmin(self.bounds.size.width - 80, c.x - 10)), fmax(30, fmin(self.bounds.size.height - 44, c.y + 10)), [label UTF8String], [label length]);
//[NSString drawAtPoint:aPoint withAttributes:dictOfAttributes];
label = [NSString stringWithFormat:@"(%.0f,%.0f)", d.x, d.y];
CGContextShowTextAtPoint(ctx, fmax(30, fmin(self.bounds.size.width - 80, d.x - 10)), fmax(30, fmin(self.bounds.size.height - 44, d.y + 10)), [label UTF8String], [label length]);
label = [NSString stringWithFormat:@"(%.0f,%.0f)", b.x, b.y];
CGContextShowTextAtPoint(ctx, fmax(30, fmin(self.bounds.size.width - 80, b.x - 10)), fmax(30, fmin(self.bounds.size.height - 44, b.y + 10)), [label UTF8String], [label length]);
CGContextShowTextAtPoint(ctx, fmax(30, fmin(self.bounds.size.width - 80, a.x - 10)), fmax(30, fmin(self.bounds.size.height - 44, a.y + 10)), [label UTF8String], [label length]);
}