3

我将图层添加到我的视图中:

    [self.layer insertSublayer:_gradient above:_another.layer];

然后它似乎并没有被删除,即使我在超级层上调用needsDisplay :

        [_gradient removeFromSuperlayer];

我怎样才能删除它?

4

2 回答 2

3

从超级层中删除后,您可以设置渐变为零。它对我有用。

[gradient removeFromSuperlayer];
 gradient =nil;
于 2017-03-01T09:50:33.407 回答
2

我使用这些,然后检查以确保将其删除:

// assuming there is only 1 gradientLayer this should print 1
print(parentView.layer.sublayers?.count as Any)

parentView.layer.sublayers?.removeAll() // remove everything from the parentView
gradientLayer.removeFromSuperlayer() // just to be safe

// check to see if it's in the parentView or not
if let _ = (parentView.layer.sublayers?.compactMap { $0 as? CAGradientLayer })?.first {

    print("gradientLayer has not been removed")
} else {

    print("yay it's removed")
}

// this should now print nil
print(parentView.layer.sublayers?.count as Any)
于 2019-04-28T00:36:55.617 回答