| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- //
- // Created by Jake Lin on 12/1/15.
- // Copyright © 2015 IBAnimatable. All rights reserved.
- //
- import UIKit
- public protocol CornerDesignable: class {
- /**
- `border-radius`
- */
- var cornerRadius: CGFloat { get set }
- /**
- corner side: Top Left, Top Right, Bottom Left or Bottom Right, if not specified, all corner sides will display,
- */
- var cornerSides: CornerSides { get set }
- }
- // MARK: - UIView
- public extension CornerDesignable where Self: UIView {
- func configureCornerRadius() {
- configureCornerRadius(in: self)
- }
- }
- // MARK: - UICollectionViewCell
- public extension CornerDesignable where Self: UICollectionViewCell {
- func configureCornerRadius() {
- if !cornerRadius.isNaN && cornerRadius > 0 {
- // Remove any previous corner mask, i.e. coming from UIView type implementation
- if layer.mask?.name == "cornerSideLayer" {
- layer.mask?.removeFromSuperlayer()
- }
- layer.cornerRadius = 0.0
- applyCorner(in: contentView)
- contentView.layer.masksToBounds = true
- } else {
- contentView.layer.masksToBounds = false
- }
- }
- }
- // MARK: - Common
- extension CornerDesignable {
- func configureCornerRadius(in view: UIView) {
- guard !cornerRadius.isNaN && cornerRadius > 0 else {
- return
- }
- applyCorner(in: view)
- }
- private func applyCorner(in view: UIView) {
- if cornerSides == .allSides {
- view.layer.cornerRadius = cornerRadius
- } else {
- view.layer.cornerRadius = 0.0
- // if a layer mask is specified, remove it
- view.layer.mask?.removeFromSuperlayer()
- view.layer.mask = cornerSidesLayer(inRect: view.bounds)
- }
- }
- private func cornerSidesLayer(inRect bounds: CGRect) -> CAShapeLayer {
- let cornerSideLayer = CAShapeLayer()
- cornerSideLayer.name = "cornerSideLayer"
- cornerSideLayer.frame = CGRect(origin: .zero, size: bounds.size)
- let cornerRadii = CGSize(width: cornerRadius, height: cornerRadius)
- let roundingCorners: UIRectCorner = cornerSides.rectCorner
- cornerSideLayer.path = UIBezierPath(roundedRect: bounds,
- byRoundingCorners: roundingCorners,
- cornerRadii: cornerRadii).cgPath
- return cornerSideLayer
- }
- }
|