![](/img/trans.png)
[英]How to apply different corner radius to different corners of UIView in iOS Swift?
[英]How to make different rounded corner values in UIView dynamically in iOS?
我想对我的观点做出不同的修改。 我的视图正在动态加载。 例如:我有3个视图,并希望为每个视图传递不同的角值。
视图1:topLeft:12,bottomLeft:12,topRight:4,bottomRight:4
View2:topLeft:4,bottomLeft:4,topRight:12,bottomRight:12
View3:topLeft:4,bottomLeft:12,topRight:4,bottomRight:4
样例代码:
extension UIView{
enum Corner:Int {
case bottomRight = 0,
topRight,
bottomLeft,
topLeft
}
private func parseCorner(corner: Corner) -> CACornerMask.Element {
let corners: [CACornerMask.Element] = [.layerMaxXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
return corners[corner.rawValue]
}
private func createMask(corners: [Corner]) -> UInt {
return corners.reduce(0, { (a, b) -> UInt in
return a + parseCorner(corner: b).rawValue
})
}
func roundCorners(corners: [Corner], amount: CGFloat) {
layer.cornerRadius = amount
let maskedCorners: CACornerMask = CACornerMask(rawValue: createMask(corners: corners))
if #available(iOS 11.0, *) {
layer.maskedCorners = maskedCorners
} else {
// Fallback on earlier versions
layer.cornerRadius = 8 // Setting static values for the below versions
}
}
}
cardView.roundCorners(corners: [.topRight, .bottomRight], amount: 12).
我想传递角值数组而不是单个值。 希望通过这样的比率:[12,4,12,4]。
相反,如何制作一个结构。
struct CornerRadius {
var topLeft: CGFloat = 0
var topRight: CGFloat = 0
var bottomLeft: CGFloat = 0
var bottomRight: CGFloat = 0
init(_ topLeft: CGFloat, _ topRight: CGFloat, _ bottomLeft: CGFloat, _ bottomRight: CGFloat) {
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
}
}
let radius = CornerRadius(0, 8, 10, 11)
我不确定这是否是您想要的答案。
希望它能对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.