使用 drawRect 绘制到我自己的 UIView

Drawing to my own UIView with drawRect

这里就不多说了。就是这样,没有 STROKE 被添加到我的 UIView 中。

#import "DrawingLayerView.h"

@implementation DrawingLayerView

UIBezierPath *newPath;

- (void)startTouch:(CGPoint)point;
{
    [newPath moveToPoint:point];
}

- (void)drawRect:(CGRect)rect {
    [[UIColor redColor] setStroke];
    [newPath stroke];
}

- (void)drawShape:(CGPoint)point
{
    [newPath addLineToPoint: point]; // (4)
    [self setNeedsDisplay];
}

- (void)endTouch:(CGPoint)point {
    [newPath removeAllPoints];
}

@end

DrawingLayerView.h 是我的 UIView 的自定义 class。

肯定调用了drawRect函数,但是没有画笔画。

如果需要更多信息。告诉我,我会得到它!

您可能想要显示 how/where 您正在呼叫 drawShape。这里没有任何东西可以调用它。此外,假设您正在这样做,您似乎从未实例化过 UIBezierPath,例如:

- (void)startTouch:(CGPoint)point         // note, no semicolon
{
    newPath = [UIBezierPath bezierPath];  // note, create a `UIBezierPath` before you try to `moveToPoint`, or later, `addLineToPoint`
    [newPath moveToPoint:point];
}

代码运行至少需要一件事是 newPath 的初始化。在开始触摸时:self.newPath = [UIBezierPath bezierPath];(将其设置为 属性 以使用 self.newPath,否则您将隐式声明它为静态)。