【问题标题】:How don't clear UIView when I draw?绘制时如何不清除 UIView?
【发布时间】:2014-03-21 08:26:59
【问题描述】:

我在 UIView 上画了一条线。当我调用 -setNeedsDisplay 时,我的视图变得清晰并绘制新线。如何继续当前线路?以及如何用动画绘图?谢谢。

- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [_color CGColor]);
    CGContextSetLineWidth(context, 10.0);
    CGContextMoveToPoint(context, _startPointX, 0);
    CGContextAddLineToPoint(context, _endPointX, 0);
    CGContextStrokePath(context);
}

【问题讨论】:

    标签: ios objective-c drawing cgcontext


    【解决方案1】:

    setNeedsDisplay 完全重绘视图。因此,您需要将所有图纸存储在某个地方并在每次重绘时应用它。 你在动画绘图下是什么意思?动画绘图就是绘图本身。只需在每次小的更改后重新绘制视图,它就会看起来像您自然绘制的一样。

    【讨论】:

    • 嗨,我知道 setNeedsDisplay 方法重绘了整个视图。但我无法获得当前上下文,除了 drawRect 方法?是真的吗?
    • 是的,您无法从 drawRect 中获取当前 UIView 上下文。您只能使用 UIGraphicsBeginImageContextWithOptions 等方法创建新的。
    • 哦,实际上我是在滚动视图中绘图,并且每次绘图都会附加。正如你所说的 setNeedsDisply 方法重绘。我正在寻找另一种绘制方式,除了 CGContext 和 drawRect。谢谢你。
    • 可能是 BezierPath 和自定义绘制方法。
    【解决方案2】:

    对于动画,您可以使用这些线条创建 CAShapeLayer。

    然后用 keypath @"storkeEnd" 创建一个 CABasicAnimation toValue 设置@1 以绘制所有从清晰到绘制的线条。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      相关资源
      最近更新 更多