CornerRadius cornerごとのアニメーション

Updated
Apr 28, 2021 9:51 AM
Created
Apr 28, 2021 9:05 AM
Tags
SwiftUIKit
Keywords
Date

private final class ComposedCornerView: CodeBasedView {

    private let topRightMaskView = UIView()
    private let topLeftMaskView = UIView()
    private let bottomRightMaskView = UIView()
    private let bottomLeftMaskView = UIView()

    private let topRightView = UIView()
    private let topLeftView = UIView()
    private let bottomRightView = UIView()
    private let bottomLeftView = UIView()

    init() {
      super.init(frame: .zero)

      [
        topRightMaskView,
        topLeftMaskView,
        bottomRightMaskView,
        bottomLeftMaskView,
      ].forEach {
        $0.backgroundColor = .black
      }

      [
        topRightView,
        topLeftView,
        bottomRightView,
        bottomLeftView,
      ].forEach {
        addSubview($0)
        $0.backgroundColor = .black
      }

      topRightView.layer.maskedCorners = .layerMaxXMinYCorner
      topLeftView.layer.maskedCorners = .layerMinXMinYCorner
      bottomRightView.layer.maskedCorners = .layerMaxXMaxYCorner
      bottomLeftView.layer.maskedCorners = .layerMinXMaxYCorner

      topRightView.mask = topRightMaskView
      topLeftView.mask = topLeftMaskView
      bottomRightView.mask = bottomRightMaskView
      bottomLeftView.mask = bottomLeftMaskView

    }

    override func layoutSubviews() {
      super.layoutSubviews()

      [
        topRightView,
        topLeftView,
        bottomRightView,
        bottomLeftView,
      ].forEach {
        $0.frame = bounds
      }

      let width = bounds.width / 2
      let height = bounds.height / 2

      topRightMaskView.frame = .init(x: width, y: 0, width: width, height: height)
      topLeftMaskView.frame = .init(x: 0, y: 0, width: width, height: height)
      bottomRightMaskView.frame = .init(x: width, y: height, width: width, height: height)
      bottomLeftMaskView.frame = .init(x: 0, y: height, width: width, height: height)

    }

    @available(iOS 13, *)
    func setCornerCurve(_ curve: CALayerCornerCurve) {

      [
        topRightView,
        topLeftView,
        bottomRightView,
        bottomLeftView,
      ].forEach {
        $0.layer.cornerCurve = curve
      }

    }

    func setCorner(radius: CGFloat, mask: CACornerMask) {

      let actualRadius = radius

      if mask.contains(.layerMinXMinYCorner) {
        topLeftView.layer.cornerRadius = actualRadius
      } else {
        topLeftView.layer.cornerRadius = 0
      }

      if mask.contains(.layerMaxXMinYCorner) {
        topRightView.layer.cornerRadius = actualRadius
      } else {
        topRightView.layer.cornerRadius = 0
      }

      if mask.contains(.layerMinXMaxYCorner) {
        bottomLeftView.layer.cornerRadius = actualRadius
      } else {
        bottomLeftView.layer.cornerRadius = 0
      }

      if mask.contains(.layerMaxXMaxYCorner) {
        bottomRightView.layer.cornerRadius = actualRadius
      } else {
        bottomRightView.layer.cornerRadius = 0
      }
    }

  }