如何在底部有一个带有线角而不是圆角的 UIView
How to have a UIView with line corners instead of rounded corners in the bottom
我想用这个形状创建一个 UIView,它有线角。我怎样才能用 UIBezierPath 绘制它,或者有更简单的方法吗?任何帮助将不胜感激!
是的,您可能应该创建一条绘制该形状的路径。
可能处理器效率最高的方法是将 CAShapeLayer 作为子层安装到视图中,并将路径安装到形状层的路径中 属性。考虑以下游乐场:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
let shapeLayer = CAShapeLayer()
let dogEarValue: CGFloat = 40.0
func buildShape() {
let box = view.bounds
shapeLayer.frame = box
let path = UIBezierPath()
path.move(to: box.origin)
path.addLine(to: CGPoint(x:box.maxX, y: box.origin.y))
path.addLine(to: CGPoint(x:box.maxX, y: box.maxY - dogEarValue))
path.addLine(to: CGPoint(x:box.maxX - dogEarValue, y: box.maxY))
path.addLine(to: CGPoint(x:box.origin.x + dogEarValue, y: box.maxY))
path.addLine(to: CGPoint(x:box.origin.x, y: box.maxY - dogEarValue))
path.close()
shapeLayer.path = path.cgPath
}
override func loadView() {
let view = UIView()
view.backgroundColor = .white
shapeLayer.fillColor = UIColor.black.cgColor
shapeLayer.backgroundColor = UIColor.clear.cgColor
view.layer.addSublayer(shapeLayer)
let label = UILabel()
label.text = "Hello World"
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
}
override func viewDidLayoutSubviews() {
buildShape()
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
我想用这个形状创建一个 UIView,它有线角。我怎样才能用 UIBezierPath 绘制它,或者有更简单的方法吗?任何帮助将不胜感激!
是的,您可能应该创建一条绘制该形状的路径。
可能处理器效率最高的方法是将 CAShapeLayer 作为子层安装到视图中,并将路径安装到形状层的路径中 属性。考虑以下游乐场:
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
let shapeLayer = CAShapeLayer()
let dogEarValue: CGFloat = 40.0
func buildShape() {
let box = view.bounds
shapeLayer.frame = box
let path = UIBezierPath()
path.move(to: box.origin)
path.addLine(to: CGPoint(x:box.maxX, y: box.origin.y))
path.addLine(to: CGPoint(x:box.maxX, y: box.maxY - dogEarValue))
path.addLine(to: CGPoint(x:box.maxX - dogEarValue, y: box.maxY))
path.addLine(to: CGPoint(x:box.origin.x + dogEarValue, y: box.maxY))
path.addLine(to: CGPoint(x:box.origin.x, y: box.maxY - dogEarValue))
path.close()
shapeLayer.path = path.cgPath
}
override func loadView() {
let view = UIView()
view.backgroundColor = .white
shapeLayer.fillColor = UIColor.black.cgColor
shapeLayer.backgroundColor = UIColor.clear.cgColor
view.layer.addSublayer(shapeLayer)
let label = UILabel()
label.text = "Hello World"
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
}
override func viewDidLayoutSubviews() {
buildShape()
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()