创建可伸缩路径CGContextPath

时间:2013-09-26 09:28:24

标签: ios core-graphics cgcontextdrawimage

我是iOS新手。

使用Core Graphics / UIKit绘制时遇到问题。 我想在Window中实现类似绘画形状的功能。

我使用此来源:https://github.com/JagCesar/Simple-Paint-App-iOS,并添加新功能。

当touchesMoved时,我会根据touchesBegan中的点和当前触摸点绘制一个形状。它绘制了所有的形状。

    - (void)drawInRectModeAtPoint:(CGPoint)currentPoint
    {
    UIGraphicsBeginImageContext(self.imageViewDrawing.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [self.currentColor setFill];

    [self.imageViewDrawing.image drawInRect:CGRectMake(0, 0, self.imageViewDrawing.frame.size.width, self.imageViewDrawing.frame.size.height)];

    CGContextMoveToPoint(context, self.beginPoint.x, self.beginPoint.y);
    CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
    CGContextAddLineToPoint(context, self.beginPoint.x * 2 - currentPoint.x, currentPoint.y);
    CGContextAddLineToPoint(context, self.beginPoint.x, self.beginPoint.y);
    CGContextFillPath(context);

    self.currentImage = UIGraphicsGetImageFromCurrentImageContext();
    self.imageViewDrawing.image = self.currentImage;
    UIGraphicsEndImageContext();
    }

我的意思是,我只想创建一个形状,当touchesBegan,app记录点,touchesMoved时,形状按触摸缩放,而当touchesEnd时,将形状绘制到ImageContex

希望你能给我一些提示。 谢谢。

1 个答案:

答案 0 :(得分:0)

您可能希望从上下文中提取此功能。在使用图像时,请使用图像视图。在触摸开始时,创建图像和图像视图。将图像视图框设置为大小为{1,1}的触摸点。触摸移动时,通过更改其frame来移动/缩放图像视图。当触摸结束时,使用起点和终点将图像渲染到上下文中(应该与图像视图的最终frame相同)。

这样做意味着您不会在接收到下一次触摸更新时需要再次删除的上下文中添加任何内容。上述方法与CALayer类似,而不是图像视图。您还可以在视图上使用transform查看解决方案。

相关问题