Rounded corners have always been a common problem in development.
In order to achieve the effect of partial fillet, I went to check how to achieve with OC.
Unfortunately, the direct syntax conversion is not possible after, because mas_maskContraints (is masonry this library syntax, thanks to isaced) method in Swift I did not find. The search results in Stack Overflow are more moving:
I finally gave up and chose another implementation:
Put him in the way of Swift:
extension UIView { /// 部分圆角 /// /// - Parameters: /// - corners: 需要实现为圆角的角,可传入多个 /// - radii: 圆角半径 func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat) { let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii)) let maskLayer = CAShapeLayer() maskLayer.frame = self.bounds maskLayer.path = maskPath.cgPath self.layer.mask = maskLayer }}
One thing to be aware of when calling:
// 调用没有任何问题,将左上角与右上角设为圆角。button.corner(byRoundingCorners: [UIRectCorner.topLeft, UIRectCorner.topRight], radii: 5)// 编译错误let corners = [UIRectCorner.topLeft, UIRectCorner.topRight]button.corner(byRoundingCorners: corners, radii: 5)
You need to convert the type:
let corners: UIRectCorner = [UIRectCorner.bottomLeft, UIRectCorner.bottomRight]// 类型可省略let corners: UIRectCorner = [.bottomLeft,.bottomRight]
In the UIBerizePath class, we see that the byRoundingCorners parameter receives one UIRectCorner , not the array type, so a one-step type conversion is required, and multiple fillets are set.
In terms of performance, I simply made a 1000-row rounded button and a table with a label, which was very smooth scrolling. Off-screen rendering is possible with the coreanimation test of the instrument species. Depending on the WWDC 2014:advanced Graphics and animations for IOS Apps, the system fillet is implemented using mask, and now the hardware performance or optimization is certainly better than that of the year.
Swift realizes partial Fillet