gpt4 book ai didi

ios - 使用 CAShapeLayer 和 CAGradientLayer 提高渐变边框的性能

转载 作者:可可西里 更新时间:2023-11-01 01:02:39 26 4
gpt4 key购买 nike

我正在使用此方法将渐变边框应用于 View 。但是,当 View 位于表格 View 的单元格中时,表格 View 的滚动帧率会显着下降。有没有办法提高性能?我尝试按照 Apple 的建议将 opaque 、 drawsAsynchronously 和 shouldRasterize 设置为 true 但没有任何改变。

func addBorder(colors:[UIColor]? = nil,size:CGSize? = nil) {



_ = self.sublayers?.filter({$0.name == "GradientBorder"}).map({$0.removeFromSuperlayer()})
let shapeFrame = CGRect(origin: CGPointZero, size: size ?? bounds.size)
let gradientLayer = CAGradientLayer()
gradientLayer.name = "GradientBorder"

gradientLayer.frame = shapeFrame
gradientLayer.startPoint = CGPointMake(0.0, 0.5)
gradientLayer.endPoint = CGPointMake(1.0, 0.5)
gradientLayer.colors = colors == nil ? [UIColor.blueColor().CGColor,UIColor.redColor().CGColor] : colors!.map({$0.CGColor})
gradientLayer.contentsScale = UIScreen.mainScreen().scale


let shapeLayer = CAShapeLayer()
shapeLayer.lineWidth = 2

shapeLayer.path = UIBezierPath(roundedRect: shapeFrame, cornerRadius: self.cornerRadius).CGPath
shapeLayer.fillColor = nil
shapeLayer.strokeColor = UIColor.blackColor().CGColor


gradientLayer.shouldRasterize = true
gradientLayer.opaque = true
gradientLayer.drawsAsynchronously = true
shapeLayer.drawsAsynchronously = true
shapeLayer.opaque = true

gradientLayer.mask = shapeLayer
self.addSublayer(gradientLayer)



}

最佳答案

好的,我找到了解决方案,而且非常简单。我只是添加了这三行。

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.layer.shouldRasterize = true
self.opaque = true
self.layer.rasterizationScale = UIScreen.mainScreen().scale
}

关于ios - 使用 CAShapeLayer 和 CAGradientLayer 提高渐变边框的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34066587/

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