Jas*_*ole 5 iphone sdk layer uiview ios
我在我的图层属性中画了几行UIView.但有没有办法清理我画的所有线条?
我想清除在我的视图层上绘制的所有内容.
我用以下代码画一条线:
- (void)drawLine :(UIView *)drawInView :(CGPoint)startPosition :(CGPoint)endPosition
{
//draw the line
linePath = CGPathCreateMutable();
lineShape = [CAShapeLayer layer];
lineShape.lineWidth = 1.0f;
lineShape.lineCap = kCALineCapRound;;
lineShape.strokeColor = [[UIColor whiteColor] CGColor];
CGPathMoveToPoint(linePath, NULL, startPosition.x, startPosition.y);
CGPathAddLineToPoint(linePath, NULL, endPosition.x, endPosition.y);
lineShape.path = linePath;
CGPathRelease(linePath);
[drawInView.layer addSublayer:lineShape];
}
Run Code Online (Sandbox Code Playgroud)
我找到了一些代码来删除我绘制的所有子图层.
-(void)clearGraph :(UIView *)viewToClear
{
for (CALayer *layer in viewToClear.layer.sublayers) {
[layer removeFromSuperlayer];
}
}
Run Code Online (Sandbox Code Playgroud)
但这将给出一个例外:
2013-08-28 21:10:18.877 ServerInfo[12861:3f03] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <CALayerArray: 0x1f86b3b0> was mutated while being enumerated.'
*** First throw call stack:
(0x336ef3e7 0x3b3ea963 0x336eeec1 0x13f7d 0x158bb 0x340082fb 0x336c4857 0x336c4503 0x336c3177 0x3363623d 0x336360c9 0x33f5a5c3 0x33ffdc45 0x15843 0x34007231 0x3b8370e1 0x3b836fa8)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Run Code Online (Sandbox Code Playgroud)
我在NSTread中调用我的drawline方法.(doParsing方法具体).
NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:@selector(callTimer) object:nil];
[myThread start];
- (void)callTimer
{
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doParsing) userInfo:nil repeats:YES];
[runLoop run];
}
Run Code Online (Sandbox Code Playgroud)
Erw*_*wan 13
删除UIView的所有子层的一种更简单的方法是将其子层属性设置为nil
viewToClear.layer.sublayers = nil;
Run Code Online (Sandbox Code Playgroud)
Wai*_*ain 10
调用CGContextClearRect当前上下文和rect来清除.
根据您更新的代码,您实际上并没有绘制任何内容.你实际上是在添加子图层.因此,要删除任何上一行,您需要删除子图层.您需要找到一种方法来找到合适的子图层(可能包含属性,可能是标记)然后您可以像添加它一样删除它.
不要自己执行循环,让数组执行:
[viewToClear.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12485 次 |
| 最近记录: |