Created
February 23, 2020 13:38
-
-
Save hanishassim/7447b934595e69c379cd17b753d47611 to your computer and use it in GitHub Desktop.
Superclass to create rounded UIView (alternative to SmoothedView)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RoundedCorners.swift | |
// | |
// Created by H on 23/02/2020. | |
// Copyright © 2020 H. All rights reserved. | |
// | |
import UIKit | |
protocol RoundedCorners { | |
var cornerRadius: CGFloat { get set } | |
func applyRoundedCorners() | |
} | |
extension RoundedCorners where Self: UIView { | |
func applyRoundedCorners() { | |
let bezierPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius) | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = bezierPath.cgPath | |
layer.mask = maskLayer | |
} | |
} | |
public class BadgeView: UIView { | |
var cornerRadius: CGFloat = 20.0 | |
public override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
} | |
public override func draw(_ rect: CGRect) { | |
applyRoundedCorners() | |
} | |
override public func layoutSubviews() { | |
super.layoutSubviews() | |
applyRoundedCorners() | |
} | |
} | |
extension BadgeView: RoundedCorners { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment