65 lines
2.3 KiB
Swift
65 lines
2.3 KiB
Swift
//
|
|
// Dropshadowable.swift
|
|
// VDS
|
|
//
|
|
// Created by Bandaru, Krishna Kishore on 16/02/24.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
protocol Dropshadowable {
|
|
|
|
var shadowColorConfiguration: AnyColorable { get set }
|
|
var shadowOpacity: CGFloat { get set }
|
|
var shadowOffset: CGSize { get set }
|
|
var shadowRadius: CGFloat { get set }
|
|
}
|
|
|
|
extension ViewProtocol where Self: UIView {
|
|
|
|
func addDropShadow(_ config: Dropshadowable) {
|
|
removeDropShadows()
|
|
layer.backgroundColor = backgroundColor?.cgColor
|
|
layer.masksToBounds = false
|
|
let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: layer.cornerRadius)
|
|
let shadowLayer = CALayer()
|
|
shadowLayer.shadowPath = shadowPath.cgPath
|
|
shadowLayer.frame = bounds
|
|
shadowLayer.position = center
|
|
shadowLayer.backgroundColor = UIColor.clear.cgColor
|
|
shadowLayer.cornerRadius = layer.cornerRadius
|
|
shadowLayer.shadowColor = config.shadowColorConfiguration.getColor(self).cgColor
|
|
shadowLayer.shadowOpacity = Float(config.shadowOpacity)
|
|
shadowLayer.shadowOffset = .init(width: config.shadowOffset.width, height: config.shadowOffset.height)
|
|
shadowLayer.shadowRadius = config.shadowRadius
|
|
shadowLayer.name = "dropShadowLayer"
|
|
shadowLayer.shouldRasterize = true
|
|
shadowLayer.rasterizationScale = UIScreen.main.scale
|
|
layer.insertSublayer(shadowLayer, at: 0)
|
|
}
|
|
|
|
func removeDropShadows() {
|
|
layer.sublayers?.removeAll { $0.name == "dropShadowLayer" }
|
|
}
|
|
|
|
func addGradientLayer(with firstColor: UIColor, secondColor: UIColor) {
|
|
removeGradientLayer()
|
|
let gradientLayer = CAGradientLayer()
|
|
gradientLayer.frame = bounds
|
|
gradientLayer.startPoint = CGPoint(x: 0, y: 1)
|
|
gradientLayer.endPoint = CGPoint(x: 1, y: 0)
|
|
gradientLayer.position = center
|
|
gradientLayer.shouldRasterize = true
|
|
gradientLayer.rasterizationScale = UIScreen.main.scale
|
|
gradientLayer.cornerRadius = layer.cornerRadius
|
|
gradientLayer.colors = [firstColor.cgColor, secondColor.cgColor]
|
|
gradientLayer.name = "gradientLayer"
|
|
layer.insertSublayer(gradientLayer, at: 0)
|
|
}
|
|
|
|
func removeGradientLayer() {
|
|
layer.sublayers?.removeAll { $0.name == "gradientLayer" }
|
|
}
|
|
}
|