我必须为我的视图创建一个遮罩布局,但我无法得到我想要获得的结果
@IBOutlet weak var viewToCrop: UIView!
let angle = CGFloat(2*M_PI/3)
func createMask() {
let mask = CAShapeLayer()
mask.frame = viewToCrop.layer.bounds
let heightButton = viewToCrop.frame.height
let lineWidth = -heightButton/tan(angle)
let width = viewToCrop.layer.frame.size.width
let height = viewToCrop.layer.frame.size.height
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, 0)
CGPathAddLineToPoint(path, nil, width, 0)
CGPathMoveToPoint(path, nil, width - lineWidth, height)
CGPathAddLineToPoint(path, nil, lineWidth, height)
CGPathAddLineToPoint(path, nil, 0, 0)
mask.path = path
viewToCrop.layer.mask = mask
}
我需要创建这样的数字:
然而,在运行代码后,我得到了矩形。 我做错了什么?
P.S。我正在研究Swift 2上的遗留项目
答案 0 :(得分:1)
我没有Swift 2编译器选项,但我会采取刺激,希望能让你接近。我认为最重要的是路径线宽度需要除以2为坐标。另一件需要注意的事情是,如果视图的高度太大,那么60就太陡了。我建议根据百分比绘制它,除非你需要精确的角度。没有Swift 2编译器,请尽我所能让我知道。
@IBOutlet weak var viewToCrop: UIView!
let angle = CGFloat(2*M_PI/3)
func createMask() {
let mask = CAShapeLayer()
mask.frame = viewToCrop.layer.bounds
let heightButton = viewToCrop.frame.width
let lineWidth = -heightButton/tan(angle)
let width = viewToCrop.layer.frame.size.width
let height = viewToCrop.layer.frame.size.height
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, 0)
CGPathAddLineToPoint(path, nil, width, 0)
CGPathMoveToPoint(path, nil, width - lineWidth/2, height)
CGPathAddLineToPoint(path, nil, lineWidth/2, height)
CGPathAddLineToPoint(path, nil, 0, 0)
mask.path = path
viewToCrop.layer.mask = mask
}