UIBezierPath 删除问题 [英] UIBezierPath Removal Issue
问题描述
我正在尝试制作一个球从用户绘制的线中弹起的游戏.绘制线的代码包含在下面并且工作正常,但是一旦球与线接触或玩家绘制新线,我将如何移除线?
I am trying to make a game where a ball is bouncing off a user drawn line. The code for drawing the line is included below and works fine but how would I remove the line once the ball makes contact with it or the player draws a new line?
path = [UIBezierPath bezierPath];
// Start Coords of Line
[path moveToPoint:CGPointMake(pos2x, pos2y)];
[path addLineToPoint:CGPointMake(pos1x, pos1y)];
// End Coords of Line
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [[UIColor whiteColor] CGColor];
shapeLayer.lineWidth = 3.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
[self.view.layer addSublayer:shapeLayer];
提前致谢!
推荐答案
当你这样说时:
[self.view.layer addSublayer:shapeLayer];
...也保持对那个形状层的引用.例如,您可能有一个属性 currentShapeLayer
:
...also keep a reference to that shape layer. For example, you might have a property currentShapeLayer
:
self.currentShapeLayer = shapeLayer;
既然您有了参考,就可以轻松删除图层:
Now that you have a reference, you can easily remove the layer:
[self.currentShapeLayer removeFromSuperlayer];
iOS 编程就是保留对您知道以后需要的东西的引用.如果路径更多,意味着更多的形状层,您将需要一种更复杂、更智能的方式来区分您要删除哪个和哪个.
Programming iOS is all about keeping references to things you know you'll need later on. If there are more paths, meaning more shape layers, you will need a more complex, intelligent way of distinguishing which is which and which one you want to remove.
这篇关于UIBezierPath 删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!