[英]How to make different rounded corner values in UIView dynamically in iOS?
I want to make the different round corners to my view. 我想对我的观点做出不同的修改。 My views are loading dynamically.
我的视图正在动态加载。 Eg: I have 3 views and want to pass the different corner values for each views.
例如:我有3个视图,并希望为每个视图传递不同的角值。
View1 : topLeft : 12 , bottomLeft : 12 , topRight : 4 , bottomRight : 4 视图1:topLeft:12,bottomLeft:12,topRight:4,bottomRight:4
View2 : topLeft : 4 , bottomLeft : 4 , topRight : 12 , bottomRight : 12 View2:topLeft:4,bottomLeft:4,topRight:12,bottomRight:12
View3 : topLeft : 4 , bottomLeft : 12 , topRight : 4 , bottomRight : 4 View3:topLeft:4,bottomLeft:12,topRight:4,bottomRight:4
Sample code: 样例代码:
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).
I want to pass the array of the corner values instead of the single value. 我想传递角值数组而不是单个值。 expecting to pass like this ratio : [12,4,12,4].
希望通过这样的比率:[12,4,12,4]。
How abount make a struct instead. 相反,如何制作一个结构。
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)
I'm not sure if it is the answer you want. 我不确定这是否是您想要的答案。
Hopes it could help you. 希望它能对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.