如何在两个重叠的 UIView 周围绘制边框
How to draw a border around two overlapping UIViews
我有两个 UIView,它们在重叠时创建了一个独特的形状,我想在合并的视图周围画一个边框。
这样做的正确方法是什么?
UIView 是:
- 圆形图像视图以显示用户个人资料图像
- 将包含用户配置文件数据(即姓名、出生日期等)的矩形视图
这是两个视图一起显示的图像:
好的,通过执行以下操作解决了这个问题:
- 在矩形 UIView 上设置边框
用UIBezierPath
创建一个CAShapeLayer
画一个半圆,并在drawRect
方法中添加到圆UIView's
层:
覆盖 func draw(_ rect: CGRect) {
super.draw(矩形)
let shapeLayer = CAShapeLayer()
let topSemiCirclePath = UIBezierPath(arcCenter: userImageView.center, radius: userImageView.bounds.size.width / 2.0, startAngle: CGFloat(Double.pi), endAngle: CGFloat(Double.pi / 180), clockwise: true)
topSemiCirclePath.lineWidth = 2.0
UIColor.lightGray.setStroke()
topSemiCirclePath.stroke()
shapeLayer.path = topSemiCirclePath.cgPath
userImageView.layer.addSublayer(shapeLayer)
}
我有两个 UIView,它们在重叠时创建了一个独特的形状,我想在合并的视图周围画一个边框。
这样做的正确方法是什么?
UIView 是:
- 圆形图像视图以显示用户个人资料图像
- 将包含用户配置文件数据(即姓名、出生日期等)的矩形视图
这是两个视图一起显示的图像:
好的,通过执行以下操作解决了这个问题:
- 在矩形 UIView 上设置边框
用
UIBezierPath
创建一个CAShapeLayer
画一个半圆,并在drawRect
方法中添加到圆UIView's
层:覆盖 func draw(_ rect: CGRect) { super.draw(矩形)
let shapeLayer = CAShapeLayer() let topSemiCirclePath = UIBezierPath(arcCenter: userImageView.center, radius: userImageView.bounds.size.width / 2.0, startAngle: CGFloat(Double.pi), endAngle: CGFloat(Double.pi / 180), clockwise: true) topSemiCirclePath.lineWidth = 2.0 UIColor.lightGray.setStroke() topSemiCirclePath.stroke() shapeLayer.path = topSemiCirclePath.cgPath userImageView.layer.addSublayer(shapeLayer)
}