簡體   English   中英

iOS清晰的UIView層

[英]iOS clear layer of UIView

我在UIView的layer屬性中畫了幾行。 但有沒有辦法清理我畫的所有線條?

我想清除在我的視圖層上繪制的所有內容。

我用以下代碼畫一條線:

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

但這將給出一個例外:

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中調用我的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];
}

刪除UIView的所有子層的一種更簡單的方法是將其子層屬性設置為nil

viewToClear.layer.sublayers = nil;

使用當前上下文調用CGContextClearRect並清除rect。


根據您更新的代碼,您實際上並沒有繪制任何內容。 你實際上是在添加子圖層。 因此,要刪除任何上一行,您需要刪除子圖層。 您將需要找到一種方法來找到適當的子圖層(可能包含屬性,可能是標記)然后您可以像添加它一樣刪除它。


不要自己執行循環,讓數組執行:

[viewToClear.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];

我使用了與Erwan的反應非常相似的東西..但事實證明我不需要'層'部分。 這對我有用。 小而有效。

imageLayer.sublayers = nil;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM