在CAShapeLayer上设置setNeedsDisplay以重绘图层

时间:2014-01-25 21:52:08

标签: ios objective-c uiview cashapelayer

我正在尝试修改CAShapeLayer的路径,然后使用setNeedsDisplay重新绘制,

但由于某种原因,它不会重绘。我可以重新绘制它的唯一方法是调用viewDidLoad,我相信这不是正确的方法。

以下是初始化代码:

self.shape.lineWidth = 2.0;
[self.shape setFillColor:[[UIColor clearColor] CGColor]];
self.shape.strokeColor = [[UIColor blackColor] CGColor];
[self.shape setPath:[self.shapePath CGPath]];
self.shape.fillRule = kCAFillModeRemoved;
[self.cuttingView.layer addSublayer:self.shape];

拖动手势的手势识别器:

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static CGPoint lastLocation;
    CGPoint location = [gesture locationInView:self.cuttingView];

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        if (![self.shapePath containsPoint:location] ) {
            gesture.enabled = NO;
        } else {
            lastLocation = location;
        }
    }

    if (gesture.state == UIGestureRecognizerStateChanged) {
        CGAffineTransform translation = CGAffineTransformMakeTranslation((location.x - lastLocation.x), (location.y - lastLocation.y));
        self.shapePath.CGPath = CGPathCreateCopyByTransformingPath(self.shapePath.CGPath, &translation);
        [self.shape setNeedsDisplay];
    }
    gesture.enabled = YES;
} 

我正在打印这些位置,但它们确实发生了变化,但屏幕上没有显示任何内容。

如何解决这个问题?

谢谢!

2 个答案:

答案 0 :(得分:2)

您正在更改self.shapePath.CGPath,但您没有更改CAShapeLayer中的路径。

而不是[self.shape setNeedsDisplay],请再次执行此操作:

[self.shape setPath:[self.shapePath CGPath]];

答案 1 :(得分:1)

您没有指定图层的路径。我将删除shapePath变量,因为它没有用(AFAICS)