如何在iOS Swift中将不同的角半径应用到UIView的不同角落?

时间:2018-05-14 08:19:45

标签: ios swift uiview cornerradius

我想使用UIView将不同的角半径值应用于iOSswift的不同角落。这意味着topLeft, topRight, bottomLeft, bottomRight的{​​{1}}角应该具有不同 UIView(即1,5,10,15)。怎么实现呢?

我尝试以相同的方式应用cornerRadius,但它会覆盖之前的cornerRadius值。

2 个答案:

答案 0 :(得分:1)

如果您的部署目标是iOS 10以上,则应使用 maskedCorners

if #available(iOS 11.0, *) {
    customeView.clipsToBounds = true
    customeView.layer.cornerRadius = 5
    customeView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMinYCorner]
} 

否则尝试

let path = UIBezierPath(roundedRect: customeView.bounds, byRoundingCorners:[.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 5, height: 5))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            customeView.layer.mask = mask

答案 1 :(得分:0)

尝试这个

let rectShape = CAShapeLayer()
    rectShape.bounds = self.myView.frame
    rectShape.position = self.myView.center
    rectShape.path = UIBezierPath(roundedRect: self.myView.bounds, byRoundingCorners: [.bottomLeft , .bottomRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath
    self.myView.layer.backgroundColor = UIColor.green.cgColor
    self.myView.layer.mask = rectShape