简体   繁体   English

如何知道要删除哪个子层?

[英]How to know which subLayer to remove?

I have the bellow line of code which is meant to prevent a problem where a sublayer is displayed over another posts content.我有以下代码行,旨在防止子图层显示在另一个帖子内容上的问题。 The method in which this line is excited is called at the time of the user exiting a post.在用户退出帖子时调用该行被激发的方法。

newBaseP3FolderLayer.layer.sublayers?.remove(at: 1)

But now, I think as a result of this code, there are instances where another video will all of a sudden cover all other images.但是现在,我认为由于这段代码,在某些情况下,另一个视频会突然覆盖所有其他图像。

I noticed that this only happens when the number of subLayers is 14. (This also may be true for numbers above this) while printing the number of sublayers when leaving a post I noticed that the code worked fine (for vals like 10 or 11 and 12) until the number of subviews went to 14.我注意到这仅在子层数为 14 时发生。(这对于高于此的数字也可能是正确的)在发表帖子时打印子层数时我注意到代码工作正常(对于像 10 或 11 这样的 vals 和12) 直到子视图的数量达到 14。

How can I fix this?我怎样才能解决这个问题?

This would be better approach这将是更好的方法

for sublayer in sublayers {
    if sublayer.name == "yourLayerName" {
        sublayer.removeFromSuperlayer()
    }
}

Store the reference to the layer you want to remove while creating it and use that reference to remove the layer from its superLayer , ie存储referencelayer你想要创建的同时去除,并使用该reference删除layersuperLayer ,即

var yourLayer: CALayer?

view.layer.sublayers?.forEach({ (layer) in
    if layer == yourLayer {
        yourLayer?.removeFromSuperlayer()
    }
})

If you don't want to use layer names, try logging this:如果您不想使用图层名称,请尝试记录以下内容:

NSLog(@"%@",self.playerLayer.player.currentItem.asset);

You will see the following:您将看到以下内容:

AVURLAsset: 0x60000063bac0, URL = file:///Temp/Loops/088_JB_HD.mov AVURLAsset:0x60000063bac0,URL = file:///Temp/Loops/088_JB_HD.mov

You can then identify the actual name and URL of your asset.然后,您可以确定资产的实际名称和 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM