每个角落都有不同的cornerRadius Swift 3 - iOS [英] Different cornerRadius for each corner Swift 3 - iOS
问题描述
我想在 Swift -3 中为视图设置不同的角半径,我可以将每个角的半径设置为与以下帖子中提到的相同的值,如何为仅左上角和UIView 的右上角?
I want to set different corner radius for a view in Swift -3 , I am able to set the radius for the each corner to the same value like the one mentioned in the following post ,how to set cornerRadius for only top-left and top-right corner of a UIView?
有没有办法按照以下格式设置拐角半径?左上角半径:18半径右上角:18右下角半径:3半径左下:18
Is there a way I can set the corner radius in the following format ? Radius top-left: 18 Radius top-right: 18 Radius bottom-right: 3 Radius bottom-left: 18
推荐答案
您可以将默认的 layer.cornerRadius
设置为最小值,然后将图层蒙版的边框设置为较大的值.
You could set the default layer.cornerRadius
to the smallest value and then set the layer mask's border to the bigger value.
let demoView = UIView(frame: CGRect(x: 100, y: 200, width: 100, height: 100))
demoView.backgroundColor = UIColor.red
demoView.layer.cornerRadius = 3.0
let maskPath = UIBezierPath(roundedRect: demoView.bounds,
byRoundingCorners: [.topLeft, .topRight, .bottomLeft],
cornerRadii: CGSize(width: 18.0, height: 0.0))
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.cgPath
demoView.layer.mask = maskLayer
view.addSubview(demoView)
这篇关于每个角落都有不同的cornerRadius Swift 3 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!