如何在iOS中动态地在UIView中制作不同的圆角值?

时间:2019-06-07 03:07:13

标签: ios swift cocoa-touch uiview

我想对视图进行不同的处理。我的视图正在动态加载。例如:我有3个视图,并希望为每个视图传递不同的角值。

View1: 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]。

1 个答案:

答案 0 :(得分:0)

如何讲究构造结构。

before_action :initialize_variables, only: [:index, :create] // <- Here add the methods which require these variables

def index

    @calibration = Calibration.new
    @blank = Blank.new
    @check = Check.new

    @last_calibration = Calibration.where(user_id: current_user.id).order(created_at: :asc).last
    @last_blank = Blank.where(user_id: current_user.id).order(created_at: :asc).last
    @last_check = Check.where(user_id: current_user.id).order(created_at: :asc).last

end

我不确定这是否是您想要的答案。

希望它可以为您提供帮助。