Ios Swift3环绕UIView的两个角

Ios Swift3环绕UIView的两个角,ios,swift3,xcode8,Ios,Swift3,Xcode8,我知道这个问题已经问了很多次了,他们的解决方案也有,但对我来说什么都不管用。 我使用的是Swift3,我想在UIView的任意两个面上绕一圈。为此,我找到了以下解决方案 下面是上述解决方案的代码片段 extension UIView { /** Rounds the given set of corners to the specified radius - parameter corners: Corners to round - parameter

我知道这个问题已经问了很多次了,他们的解决方案也有,但对我来说什么都不管用。 我使用的是Swift3,我想在UIView的任意两个面上绕一圈。为此,我找到了以下解决方案

下面是上述解决方案的代码片段

extension UIView {

    /**
     Rounds the given set of corners to the specified radius

     - parameter corners: Corners to round
     - parameter radius:  Radius to round to
     */
    func round(corners: UIRectCorner, radius: CGFloat) -> Void {
         layer.addSublayer(_round(corners: corners, radius: radius))
    }

    /**
     Rounds the given set of corners to the specified radius with a border

     - parameter corners:     Corners to round
     - parameter radius:      Radius to round to
     - parameter borderColor: The border color
     - parameter borderWidth: The border width
     */
    func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
        let mask = _round(corners: corners, radius: radius)
        addBorder(mask: mask, borderColor: borderColor, borderWidth: borderWidth)
    }

    /**
     Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border

     - parameter diameter:    The view's diameter
     - parameter borderColor: The border color
     - parameter borderWidth: The border width
     */
    func fullyRound(diameter: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
        layer.masksToBounds = true
        layer.cornerRadius = diameter / 2
        layer.borderWidth = borderWidth
        layer.borderColor = borderColor.cgColor;
    }

}

private extension UIView {

    func _round(corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
        return mask
    }

    func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) {
        let borderLayer = CAShapeLayer()
        borderLayer.path = mask.path
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.lineWidth = borderWidth
        borderLayer.frame = bounds
        layer.addSublayer(borderLayer)
    }

}
这就是我得到的结果。我不知道我做错了什么。 谁能解决这个问题


代码在Swift 3中测试并运行

使用下面的
扩展
创建
圆角

extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
    let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    mask.path = path.cgPath
    self.layer.mask = mask
}
}
用法:
viewDidLoad

yourViewName.roundCorners(corners: [.topRight, .bottomLeft], radius: 10)
输出:


首先,您需要为您需要的文本字段或按钮创建IBOutlet 需要拐弯

  • 转到ViewDidLoad()方法
  • 写下IBOutLet的名称
  • 示例代码mybutton.layer.cornerRadius=10
  • 这是你问题的解决办法

  • 给我们看一些代码这是最简短的:)在viewDidLoad()中调用roundCorners()在视图布局之前应用掩码,不考虑将来的布局更改。如果要从ViewController调用它,应该在viewDidLayoutSubviews()中调用它。另一个选项是创建一个UIView子类,其中包含角点和半径的变量,用于更新布局子视图中的遮罩(),他特别问你如何仅圆角两个角,你的答案设置所有角点的角点半径。