Ios SetNeeds在CAShapeLayer上显示,以便重新绘制图层
我正在尝试修改我的Ios SetNeeds在CAShapeLayer上显示,以便重新绘制图层,ios,objective-c,uiview,cashapelayer,Ios,Objective C,Uiview,Cashapelayer,我正在尝试修改我的CAShapeLayer的路径,然后使用setNeedsDisplay重新绘制它 但由于某些原因,它不会重新绘制。我唯一能让它重画的方法是调用viewDidLoad,我确信这不是正确的方法 以下是初始化代码: self.shape.lineWidth = 2.0; [self.shape setFillColor:[[UIColor clearColor] CGColor]]; self.shape.strokeColor = [[UIColor blackColor] CGC
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;
}
我正在打印位置,它们确实发生了变化,但屏幕上什么也没有显示
这怎么能解决呢
谢谢 您正在更改
self.shapePath.CGPath
,但您没有在CAShapeLayer
中更改路径
再次执行以下操作,而不是[self.shape setNeedsDisplay]
:
[self.shape setPath:[self.shapePath CGPath]];
不将路径指定给图层。我会删除shapePath变量,因为它没有任何用处(AFAICS)