【问题标题】:How to 'record' a UIBezierPath to be able to playback a stroked drawing path如何“记录” UIBezierPath 以便能够回放描边的绘图路径
【发布时间】:2013-12-26 07:39:56
【问题描述】:

在我当前的 iOS 应用程序中,用户可以通过使用 UIBezierPath 平滑路径来用手指绘制;然而,这很简单。我想知道的是,当用户抬起手指并更改铅笔颜色时,是否可以记录路径、点以及与路径和点相关的颜色。然后我的目标是播放按钮将实时播放他们刚刚创建的所有内容,并通过动画加快速度,以防他们花费几分钟绘制。

感谢您的回复。这是我目前用于绘图的代码(不是最好的代码):

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

【问题讨论】:

  • 是的,这是可能的。

标签: ios objective-c ios7 uibezierpath


【解决方案1】:

我要做的是创建一个自定义对象来代表您的绘图的单个“段”。 (我们称它为“BezierSegment”。)快速浏览一下,您似乎正在使用二次贝塞尔线段。因此,创建一个对象来保存贝塞尔曲线的 3 个控制点和用于绘制它的颜色。每次绘制新的“段”时,创建这些对象之一并将其添加到段对象的可变数组中。

然后您可以遍历 BezierSegment 对象数组,从每个对象中创建 BezierPath 对象,然后将其绘制到屏幕上以便重新创建。

您还可以保存线条粗细、可选的封闭路径以及单独的笔颜色等。

【讨论】:

  • 谢谢邓肯,这应该是一些值得深思的好东西!编辑:我会在触摸移动和结束方法中添加“段”吗?
  • 我没有仔细考虑过。在我的脑海中,我会说是的,在你的代码中的同一位置添加段,你现在正在创建 CGPaths 和绘图。
  • Duncan,难道没有必要将与段相关的颜色存储到字典中吗?每个字典将由与其关联的颜色、笔划宽度和实际段组成。最后,将每个字典添加到 NSMutableArray?还是不需要字典?
  • 编辑:现在我想到了,您只需将颜色、笔触宽度和路径添加到数组中,然后将这些数组添加到整个“容器”NSMutableArray?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
相关资源
最近更新 更多