【发布时间】:2019-02-24 07:52:29
【问题描述】:
我想做这样的用户界面
我希望这个视图上有圆角和阴影。
这是我在 Xib 中的视图层次结构:
-Content View
- Shadow View
- Container View
- Left View
- Concave View
- Action View
这是我实现当前 UI 的 sn-p:
对于凹形路径:
let couponPath = UIBezierPath()
let zeroPoint = CGPoint.zero
couponPath.move(to: zeroPoint)
couponPath.addLine(to: CGPoint(x: leftView.frame.width, y: zeroPoint.y))
let radius = concaveView.frame.width / 2
let centerX = zeroPoint.x + leftView.frame.width + radius
couponPath.addArc(withCenter: CGPoint(x: centerX, y: zeroPoint.y), radius: radius, startAngle: CGFloat.pi, endAngle: 0, clockwise: false)
couponPath.addLine(to: CGPoint(x: containerView.frame.width, y: zeroPoint.y))
couponPath.addLine(to: CGPoint(x: containerView.frame.width, y: containerView.frame.height))
couponPath.addArc(withCenter: CGPoint(x: leftView.frame.width + radius, y: containerView.frame.height), radius: radius, startAngle: CGFloat.pi * 0, endAngle: CGFloat.pi, clockwise: false)
couponPath.addLine(to: CGPoint(x: zeroPoint.x, y: containerView.frame.height))
couponPath.close()
然后我创建CALayer,路径是couponPath。
let shapeLayer = CAShapeLayer()
shapeLayer.path = couponPath.cgPath
我将它隐藏到我的 containerView 中:
self.containerView.layer.mask = shapeLayer
我尝试使用此代码添加阴影。
let shadowLayer = CAShapeLayer()
shadowLayer.frame = containerView.frame
shadowLayer.path = shapeLayer.path
shadowLayer.shadowOpacity = 0.5
shadowLayer.shadowRadius = 5
shadowLayer.masksToBounds = false
shadowLayer.shadowOffset = CGSize(width: 0, height: 20)
self.shadowView.layer.addSublayer(shadowLayer)
如何添加另一个遮罩来拐角容器视图的半径? 有没有更好的方法来添加阴影而不是使用新视图(ShadowView)并在其上应用阴影?
这是我的 xib 和视图 Gist
谢谢。
【问题讨论】:
-
这很有可能:stackoverflow.com/a/57465440/294884 会有所帮助。我不是 100% 了解您的需求,但这是基本的“技巧”。
标签: swift calayer uibezierpath