gpt4 book ai didi

iphone - iOS清除UIView层

转载 作者:可可西里 更新时间:2023-11-01 04:05:03 25 4
gpt4 key购买 nike

我在 UIView 的图层属性中画了几行。但是有没有一种方法可以把我画的线全部清理干净呢?

我想清除在 View 层上绘制的所有内容。

我用下面的代码画了一条线:

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

我找到了一些代码来删除我绘制的所有子图层。

-(void)clearGraph :(UIView *)viewToClear
{
for (CALayer *layer in viewToClear.layer.sublayers) {
[layer removeFromSuperlayer];
}
}

但这会给出一个异常(exception):

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)

我在 NSTread 中调用我的画线方法。 (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];
}

最佳答案

删除 UIView 的所有子层的更简单方法是将其子层属性设置为 nil

viewToClear.layer.sublayers = nil;

关于iphone - iOS清除UIView层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com