Ios 我想在Swift中的ScrollView内的底部创建UIView圆形形状
不在顶部边缘。我想这样做的底部边缘和内部滚动视图。Ios 我想在Swift中的ScrollView内的底部创建UIView圆形形状,ios,swift,iphone,swift4.2,Ios,Swift,Iphone,Swift4.2,不在顶部边缘。我想这样做的底部边缘和内部滚动视图。 如何执行此操作创建扩展,如下所示 extension UIView { func round(corners: UIRectCorner, cornerRadius: Double) { let size = CGSize(width: cornerRadius, height: cornerRadius) let bezierPath = UIBezierPath(roundedRect: self
如何执行此操作创建扩展,如下所示
extension UIView {
func round(corners: UIRectCorner, cornerRadius: Double) {
let size = CGSize(width: cornerRadius, height: cornerRadius)
let bezierPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: size)
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.bounds
shapeLayer.path = bezierPath.cgPath
self.layer.mask = shapeLayer
}
}
你可以这样使用它
self.myView.round(corners: [.bottomLeft, .bottomRight], cornerRadius: 35)
我希望它对你有用…:
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
把它当作
yourView.roundCorners(corners : [.bottomLeft, .bottomRight], radius : 20 )
另外,在viewDidLayoutSubviews中更新布局,否则您可能会在soem设备中面临剪裁 在视图层上配置遮罩角和拐角半径,即
这应该接近你想要的。如果贝塞尔路径点不太正确,请使用它:
@IBDesignable
class MyRoundBottomView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
let y = bounds.size.height - 80.0
let p1 = CGPoint(x: 0.0, y: y)
let p2 = CGPoint(x: bounds.size.width, y: y)
let cp1 = CGPoint(x: p1.x, y: bounds.size.height)
let cp2 = CGPoint(x: bounds.size.width, y: bounds.size.height)
let myBez = UIBezierPath()
myBez.move(to: CGPoint(x: 0.0, y: y))
myBez.addCurve(to: p2, controlPoint1: cp1, controlPoint2: cp2)
myBez.addLine(to: CGPoint(x: bounds.size.width, y: 0.0))
myBez.addLine(to: CGPoint.zero)
myBez.close()
let l = CAShapeLayer()
l.path = myBez.cgPath
layer.mask = l
}
}
它被标记为@IBDesignable,因此您可以在故事板中添加UIView,并将其自定义类指定给MyRoundBottomView,您将在设计时看到它:
检查这个链接-这是为我工作的视图在内部滚动视图也。谢谢兄弟
@IBDesignable
class MyRoundBottomView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
let y = bounds.size.height - 80.0
let p1 = CGPoint(x: 0.0, y: y)
let p2 = CGPoint(x: bounds.size.width, y: y)
let cp1 = CGPoint(x: p1.x, y: bounds.size.height)
let cp2 = CGPoint(x: bounds.size.width, y: bounds.size.height)
let myBez = UIBezierPath()
myBez.move(to: CGPoint(x: 0.0, y: y))
myBez.addCurve(to: p2, controlPoint1: cp1, controlPoint2: cp2)
myBez.addLine(to: CGPoint(x: bounds.size.width, y: 0.0))
myBez.addLine(to: CGPoint.zero)
myBez.close()
let l = CAShapeLayer()
l.path = myBez.cgPath
layer.mask = l
}
}