2022-01-27 14:23:39 +01:00
|
|
|
//
|
|
|
|
// UIView.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-1-17.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
extension UIView {
|
|
|
|
public static var isZoomedMode: Bool {
|
|
|
|
return UIScreen.main.scale != UIScreen.main.nativeScale
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
extension UIView {
|
|
|
|
@discardableResult
|
|
|
|
public func applyCornerRadius(radius: CGFloat) -> Self {
|
|
|
|
layer.masksToBounds = true
|
|
|
|
layer.cornerRadius = radius
|
|
|
|
layer.cornerCurve = .continuous
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
public func applyShadow(
|
|
|
|
color: UIColor,
|
|
|
|
alpha: Float,
|
|
|
|
x: CGFloat,
|
|
|
|
y: CGFloat,
|
|
|
|
blur: CGFloat,
|
|
|
|
spread: CGFloat = 0
|
|
|
|
) -> Self {
|
|
|
|
layer.masksToBounds = false
|
|
|
|
layer.shadowColor = color.cgColor
|
|
|
|
layer.shadowOpacity = alpha
|
|
|
|
layer.shadowOffset = CGSize(width: x, height: y)
|
|
|
|
layer.shadowRadius = blur / 2.0
|
|
|
|
if spread == 0 {
|
|
|
|
layer.shadowPath = nil
|
|
|
|
} else {
|
|
|
|
let dx = -spread
|
|
|
|
let rect = bounds.insetBy(dx: dx, dy: dx)
|
|
|
|
layer.shadowPath = UIBezierPath(rect: rect).cgPath
|
|
|
|
}
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 17:45:27 +01:00
|
|
|
|
|
|
|
public extension UIView {
|
2022-12-03 02:35:11 +01:00
|
|
|
@discardableResult
|
2022-11-26 15:36:57 +01:00
|
|
|
func pinToParent(padding: UIEdgeInsets = .zero) -> [NSLayoutConstraint] {
|
|
|
|
pinTo(to: self.superview, padding: padding)
|
2022-11-17 17:45:27 +01:00
|
|
|
}
|
2022-12-03 02:35:11 +01:00
|
|
|
|
|
|
|
@discardableResult
|
2022-11-26 15:36:57 +01:00
|
|
|
func pinTo(to view: UIView?, padding: UIEdgeInsets = .zero) -> [NSLayoutConstraint] {
|
2022-12-03 02:35:11 +01:00
|
|
|
guard let pinToView = view else { return [] }
|
|
|
|
let constraints = [
|
2022-11-26 15:36:57 +01:00
|
|
|
topAnchor.constraint(equalTo: pinToView.topAnchor, constant: padding.top),
|
|
|
|
leadingAnchor.constraint(equalTo: pinToView.leadingAnchor, constant: padding.left),
|
|
|
|
trailingAnchor.constraint(equalTo: pinToView.trailingAnchor, constant: -padding.right),
|
|
|
|
bottomAnchor.constraint(equalTo: pinToView.bottomAnchor, constant: -padding.bottom),
|
2022-12-03 02:35:11 +01:00
|
|
|
]
|
|
|
|
NSLayoutConstraint.activate(constraints)
|
|
|
|
return constraints
|
2022-11-17 17:45:27 +01:00
|
|
|
}
|
|
|
|
}
|