UIBezierPath Removal Issue

让人想犯罪 __ 提交于 2019-12-11 11:09:46

问题


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];

Thanks in advance!


回答1:


When you say this:

[self.view.layer addSublayer:shapeLayer];

...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];

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.



来源:https://stackoverflow.com/questions/23507359/uibezierpath-removal-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!