Swift:删除图层,而不删除UITableViewCell中的UIView [英] Swift: remove layer, without removing UIView in UITableViewCell

查看:158
本文介绍了Swift:删除图层,而不删除UITableViewCell中的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UITableViewCell,其中包含 UIView
我做了一些CABasicAnimation并将它附加到新的CAShapeLayer,然后我将这个图层添加到我的UITableViewCell中的超级图层:

I have UITableViewCell with UIView in it. I made some CABasicAnimation and attach it to new CAShapeLayer, then I add this layer to my super layer in my UITableViewCell:

self.layer.addSublayer(myLayer!)

除了myLayer(和他的动画)之外,一切都很好显示在我的 UIView 之上。
我希望该标签低于 UIView

All nice except that myLayer (and his animation) showing above my UIView. I want that label be below UIView.

我通过添加<$ c $实现此目的c> UIView 图层的方式相同:

I achieve this by adding my UIView layer the same way:

self.layer.addSublayer(myViewLayer!)

在这种情况下,我的 UIView 图层位于带动画的CAShapeLayer顶部。

In this case, my UIView layer be on the top of the CAShapeLayer with animation.

但是我有问题,我需要删除UIView层 - myViewLayer 因为它在滚动时违反了UIView的宽度。

But I have a problem, I need to remove layer of UIView - myViewLayer because it violates width of the UIView when scroll.

当动画完成,我需要删除图层时,我可以毫无困难地删除CAShapeLayer - myLayer。

When animation is done, and I need to remove layers, I can remove CAShapeLayer - myLayer without troubles.

myLayer?.removeFromSuperlayer()

但是当我尝试用 myViewLayer 做同样的事情时,我的UIView也被删除了。但我不希望这样,我需要在屏幕上和我的视图层次结构中使用我的UIView。

But when I try to do the same with myViewLayer, my UIView removed too. But I don't want that, I need my UIView on screen and in my view hierarchy.

我不明白为什么,如果看 self.layer.sublayers 我看到了这一点(在添加图层之前):

I don't understand why, if look to self.layer.sublayers I see this (before adding layers):

[<CALayer: 0x7fd1f0d4fe40>, <CALayer: 0x7fd1f0ddc950>]

完成动画并移除myLayer后:

And after animation done and myLayer is removed:

[<CALayer: 0x7fd1f0d4fe40>, <CALayer: 0x7fd1f0ddc950>, <CAGradientLayer: 0x7fd1f0de3660>]

正如你所看到的,CAGradientLayer是我UIView的一层。所以,在我手动将其添加到子图层数组之前,我还没有它。我如何删除它,而不删除我的UIView?

As you can see CAGradientLayer is a layer of my UIView. So, I haven't it before I manually add it to sublayers array. How I can remove it, without removing my UIView?

或者如何在UIView下添加myLayer?

OR how can I add myLayer below UIView?

编辑

简而言之,我在动画之前有这个图层层次结构:

In few words I have this layer hierarchy before animation:

[<CALayer: 0x7fd1f0d4fe40> <- (I think this is UITableViewCell layer), <CALayer: 0x7fd1f0ddc950> <- (then, this is UITableViewCell content view layer)]

同时,我有这个视图层次结构:

In the same time, I have this view hierarchy:

UITableViewCell - >内容视图 - > UIView

UITableViewCell -> Content View -> UIView

我需要添加带动画的新图层,在UIView下面(我希望UIView部分用动画覆盖那个新图层)。我怎么能这样做?

I need to add new layer with animation, below UIView (I want that UIView partially cover that new layer with animation). How I can do this?

我在UITableView图层层次结构中没有我的UIView图层,因此我不能只使用 addLayer:下面:等等。

I haven't my UIView layer in the UITableView layer hierarchy, so I can't just add layer with animation using addLayer:below: and so on.

推荐答案

我发现了问题。我犯了一个简单的错误。我使用 self.layer.sublayers ,但我必须使用 self.contentView.layer.sublayers 这是我的错误。

I found the problem. I made a simple mistake. I used self.layer.sublayers, but I must use self.contentView.layer.sublayers this is my mistake.

当我发现这个问题时,我能够修复另一个问题并使用 addLayer:在下面:

When I found that, I was able to fix another issues and use addLayer:below:.

所以,如果你没有看到一个必须在你对象的子层中的图层,那就意味着你在错误的地方看到它也可以在哪里,而你'找到它,就像在UITableView中一样,你需要使用contentView。

So, if you don't see a layer that must be in sublayer of your object, means that you look in wrong place think where it can be also and you'll find it, like in UITableView you need to work with contentView.

这篇关于Swift:删除图层,而不删除UITableViewCell中的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆