gpt4 book ai didi

swift - bezierpath View 上的阴影为角落添加了奇怪的笔触

转载 作者:行者123 更新时间:2023-11-28 13:47:57 26 4
gpt4 key购买 nike

我想重新创建 AppStore 的“今日”卡片,其中包含圆角和浅色阴影。

我创建了一个路径、一个 maskLayer 和一个单独的 shadowLayer,根据多个消息来源,这是实现它的方式。

然而,问题是我可爱的带阴影的圆角矩形在它的角上有一些灰色笔触。我该如何解决这个问题?我尝试了不同的阴影不透明度和不同的半径。它没有解决我的问题。

在这里您可以看到我的屏幕截图和下面的代码。

override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view

// create sample view and add to view hierarchy
let bigTeaser = UIView(frame: CGRect(x: 16, y: 200, width: 343, height: 267))
bigTeaser.backgroundColor = UIColor.white
view.addSubview(bigTeaser)

// create the path for the rounded corners and the shadow
let roundPath = UIBezierPath(roundedRect: bigTeaser.bounds, cornerRadius: 20)

// create maskLayer
let maskLayer = CAShapeLayer()
maskLayer.frame = bigTeaser.bounds
maskLayer.path = roundPath.cgPath
bigTeaser.layer.mask = maskLayer

// create shadowLayer
let shadowLayer = CAShapeLayer()
shadowLayer.path = roundPath.cgPath
shadowLayer.frame = bigTeaser.frame
shadowLayer.shadowOpacity = 0.3
shadowLayer.shadowRadius = 24
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowOffset = CGSize(width: 0, height: 2)

// insert layers
bigTeaser.superview!.layer.insertSublayer(shadowLayer, below: bigTeaser.layer)

}

radius == 20

radius == 40

最佳答案

如果你替换:

shadowLayer.path = roundPath.cgPath

shadowLayer.shadowPath = roundPath.cgPath

丑陋的边框会神奇地消失。

关于swift - bezierpath View 上的阴影为角落添加了奇怪的笔触,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55160105/

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