gpt4 book ai didi

swift - 如何删除带有圆形/弧形路径的阴影 UIView 中的弧形边框?

转载 作者:行者123 更新时间:2023-11-30 11:08:17 27 4
gpt4 key购买 nike

我有这样的票证 View

TicketView

我的方法是我有2个 View ,1个是票本身,另一个是影子。我必须这样做,因为如果我遮盖 View ,它会被剪切,并且阴影不会出现在票证 View 中。

这里是创建工单 View 的代码:

let shapeLayer = CAShapeLayer()
shapeLayer.frame = someView.bounds
shapeLayer.path = UIBezierPath(roundedRect: someView.bounds,
byRoundingCorners: [UIRectCorner.bottomLeft,UIRectCorner.bottomRight] ,
cornerRadii: CGSize(width: 5.0, height: 5.0)).cgPath


let rect = CGRect(x:0, y:0, width:200, height:100)
let cornerRadius:CGFloat = 5
let subPathSideSize:CGFloat = 25

let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
let leftSubPath = UIBezierPath(arcCenter: CGPoint(x: rect.width / 2, y: 0),
radius: subPathSideSize / 2, startAngle: .pi, endAngle: .pi * 0, clockwise: false)
leftSubPath.close()

let rightSubPath = UIBezierPath(arcCenter: CGPoint(x: rect.width / 2, y: rect.height),
radius: subPathSideSize / 2, startAngle: .pi, endAngle: .pi * 0, clockwise: true)
rightSubPath.close()

path.append(leftSubPath)
path.append(rightSubPath.reversing())

let mask = CAShapeLayer()
mask.frame = shapeLayer.bounds
mask.path = path.cgPath
someView.layer.mask = mask

注意:SomeViewTicketView

这是添加阴影的代码:

let shadowMask = CAShapeLayer()
shadowMask.frame = shadowView.bounds
shadowMask.path = path.cgPath
shadowMask.shadowOpacity = 0.2
shadowMask.shadowRadius = 4
shadowMask.masksToBounds = false
shadowMask.shadowOffset = CGSize(width: 0, height: 2)

shadowView.backgroundColor = UIColor.clear
shadowView.layer.addSublayer(shadowMask)

阴影使圆弧/圆角具有像这样的边框(用红色圆圈标记)。

view with border on corner

这是我的Playground gist

你知道如何去除圆角和圆弧路径中的边框吗?

谢谢。

最佳答案

您需要在您编写的此 block 中添加clipToBounds。

let mask = CAShapeLayer()
mask.frame = shapeLayer.bounds
mask.path = path.cgPath
someView.clipsToBounds = true
someView.layer.mask = mask

关于swift - 如何删除带有圆形/弧形路径的阴影 UIView 中的弧形边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52466167/

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