Ios 如何';记录';能够播放笔划图形路径的UIBezierPath
在我当前的iOS应用程序中,用户可以通过使用带平滑路径的UIBezierPath来用手指进行绘制;然而,这很简单。我想知道的是,当用户抬起手指并改变铅笔颜色时,是否可以记录路径、点以及与路径和点相关的颜色。然后我的目标是,播放按钮将实时播放他们刚刚创建的所有内容,并通过动画加速,以防他们需要几分钟的时间来绘制 我感谢你的回答。以下是我目前用于绘图的代码(不是最好的代码):Ios 如何';记录';能够播放笔划图形路径的UIBezierPath,ios,objective-c,ios7,uibezierpath,Ios,Objective C,Ios7,Uibezierpath,在我当前的iOS应用程序中,用户可以通过使用带平滑路径的UIBezierPath来用手指进行绘制;然而,这很简单。我想知道的是,当用户抬起手指并改变铅笔颜色时,是否可以记录路径、点以及与路径和点相关的颜色。然后我的目标是,播放按钮将实时播放他们刚刚创建的所有内容,并通过动画加速,以防他们需要几分钟的时间来绘制 我感谢你的回答。以下是我目前用于绘图的代码(不是最好的代码): 我要做的是创建一个自定义对象,它表示图形的单个“段”。(我们称之为“Bezier段”。)快速看一眼,它看起来像是在使用二次B
我要做的是创建一个自定义对象,它表示图形的单个“段”。(我们称之为“Bezier段”。)快速看一眼,它看起来像是在使用二次Bezier段。因此,创建一个对象,保存贝塞尔曲线的3个控制点和用于绘制贝塞尔曲线的颜色。每次绘制新的“段”时,创建其中一个对象并将其添加到段对象的可变数组中 然后,您可以在BezierSegment对象数组中循环,从每个对象中创建BezierPath对象,并将其绘制到屏幕以重新创建
您还可以保存线条粗细、可选的封闭路径以及单独的笔颜色等内容。是的,这是可能的。谢谢Duncan,这应该是值得思考的好东西!编辑:我会在“移动”和“结束”方法中添加“分段”吗?我还没有详细考虑过。我会毫不犹豫地说,是的,在代码中创建CGPATH和绘图的同一位置添加段。Duncan,难道不需要将与段关联的颜色存储到字典中吗?每个字典将由与其相关联的颜色、笔划宽度和实际段组成。最后,将每个字典添加到NSMutableArray?或者字典是不必要的?编辑:现在我想起来了,您只需将颜色、笔划宽度和路径添加到数组中,然后将这些数组添加到整个“容器”NSMutableArray中?
@property (nonatomic, strong) UIBezierPath *path;
@property uint ctr;
@end
@implementation DrawViewController
{
CGPoint pts[4];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.ctr = 0;
UITouch *touch = [touches anyObject];
pts[0] = [touch locationInView:self.drawImage];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self.drawImage];
self.ctr++;
pts[self.ctr] = p;
if (self.ctr == 3)
{
pts[2] = CGPointMake((pts[1].x + pts[3].x)/2.0, (pts[1].y + pts[3].y)/2.0);
[self.path moveToPoint:pts[0]];
[self.path addQuadCurveToPoint:pts[2] controlPoint:pts[1]];
//[self.drawImage setNeedsDisplay];
pts[0] = pts[2];
pts[1] = pts[3];
self.ctr = 1;
dispatch_async(dispatch_get_main_queue(),
^{
UIGraphicsBeginImageContextWithOptions(self.drawImage.bounds.size, NO, 0.0);
[self.drawImage.image drawAtPoint:CGPointZero];
[[UIColor colorWithRed:self.red green:self.green blue:self.blue alpha:1.0] setStroke];
[self.path stroke];
self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.path removeAllPoints];
self.ctr = 0;
});
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.ctr == 0)
{
[self.path moveToPoint:pts[0]];
[self.path addLineToPoint:pts[0]];
}
else if (self.ctr == 1)
{
[self.path moveToPoint:pts[0]];
[self.path addLineToPoint:pts[1]];
}
else if (self.ctr == 2)
{
[self.path moveToPoint:pts[0]];
[self.path addQuadCurveToPoint:pts[2] controlPoint:pts[1]];
}
self.ctr = 0;
dispatch_async(dispatch_get_main_queue(),
^{
UIGraphicsBeginImageContextWithOptions(self.drawImage.bounds.size, NO, 0.0);
[self.drawImage.image drawAtPoint:CGPointZero];
[[UIColor colorWithRed:self.red green:self.green blue:self.blue alpha:1.0] setStroke];
[self.path stroke];
self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.path removeAllPoints];
self.ctr = 0;
});
}