Add shadow properties to ToastView

This commit is contained in:
seongho-hong
2019-08-12 16:40:01 +09:00
parent aecc46e2e5
commit ea01579cbf
3 changed files with 49 additions and 45 deletions

View File

@@ -79,10 +79,16 @@ final class RootViewController: UIViewController {
@objc dynamic func shadowButtonTouchUpInside(sender: UIButton) {
let appearance = ToastView.appearance()
if appearance.shadow == nil {
appearance.shadow = ToastShadow(color: UIColor.black.cgColor, opacity: 0.2, offset: CGSize(width: 0, height: 1), radius: 10)
if appearance.shadowColor == nil {
appearance.shadowColor = .black
appearance.shadowOpacity = 0.2
appearance.shadowOffset = CGSize(width: 0, height: 1)
appearance.shadowRadius = 10
} else {
appearance.shadow = nil
appearance.shadowColor = nil
appearance.shadowOpacity = 0
appearance.shadowOffset = .zero
appearance.shadowRadius = 0
}
}

View File

@@ -84,10 +84,16 @@
- (void)shadowButtonTouchUpInside:(UIButton *)sender {
ToastView *appearance = [ToastView appearance];
if (appearance.shadow) {
appearance.shadow = nil;
if (appearance.shadowColor) {
appearance.shadowColor = nil;
appearance.shadowOpacity = 0;
appearance.shadowOffset = CGSizeZero;
appearance.shadowRadius = 0;
} else {
appearance.shadow = [[ToastShadow alloc] initWithColor:UIColor.blackColor.CGColor opacity:@(0.2) offset:[NSValue valueWithCGSize:CGSizeMake(0, 1)] radius:@(10) path:nil];
appearance.shadowColor = [UIColor blackColor];
appearance.shadowOpacity = 0.2;
appearance.shadowOffset = CGSizeMake(0, 1);
appearance.shadowRadius = 10;
}
}

View File

@@ -1,31 +1,5 @@
import UIKit
public class ToastShadow: NSObject {
public var color: CGColor?
public var opacity: Float?
public var offset: CGSize?
public var radius: CGFloat?
public var path: CGPath?
public init(color: CGColor? = nil, opacity: Float? = nil, offset: CGSize? = nil, radius: CGFloat? = nil, path: CGPath? = nil) {
super.init()
self.color = color
self.opacity = opacity
self.offset = offset
self.radius = radius
self.path = path
}
@objc public init(color: CGColor?, opacity: NSNumber?, offset: NSValue?, radius: NSNumber?, path: CGPath?) {
super.init()
self.color = color
self.opacity = opacity?.floatValue
self.offset = offset?.cgSizeValue
self.radius = radius.flatMap { CGFloat(exactly: $0) }
self.path = path
}
}
open class ToastView: UIView {
// MARK: Properties
@@ -98,12 +72,39 @@ open class ToastView: UIView {
}
}()
/// The appearance of layer's shadow.
@objc open dynamic var shadow: ToastShadow?
/// The width ratio of toast view in window, specified as a value from 0.0 to 1.0.
/// Default value: 0.875
@objc open dynamic var maxWidthRatio: CGFloat = (280.0 / 320.0)
/// The shape of the layers shadow.
@objc open dynamic var shadowPath: CGPath? {
get { return self.layer.shadowPath }
set { self.layer.shadowPath = newValue }
}
/// The color of the layers shadow.
@objc open dynamic var shadowColor: UIColor? {
get { return self.layer.shadowColor.flatMap { UIColor(cgColor: $0) } }
set { self.layer.shadowColor = newValue?.cgColor }
}
/// The opacity of the layers shadow.
@objc open dynamic var shadowOpacity: Float {
get { return self.layer.shadowOpacity }
set { self.layer.shadowOpacity = newValue }
}
/// The offset (in points) of the layers shadow.
@objc open dynamic var shadowOffset: CGSize {
get { return self.layer.shadowOffset }
set { self.layer.shadowOffset = newValue }
}
/// The blur radius (in points) used to render the layers shadow.
@objc open dynamic var shadowRadius: CGFloat {
get { return self.layer.shadowRadius }
set { self.layer.shadowRadius = newValue }
}
// MARK: UI
@@ -114,6 +115,7 @@ open class ToastView: UIView {
self.clipsToBounds = true
return self
}()
private let textLabel: UILabel = {
let `self` = UILabel()
self.textColor = .white
@@ -198,16 +200,6 @@ open class ToastView: UIView {
width: backgroundViewSize.width,
height: backgroundViewSize.height
)
self.configureShadow()
}
private func configureShadow() {
guard let shadow = self.shadow else { return }
shadow.color.flatMap { layer.shadowColor = $0 }
shadow.opacity.flatMap { layer.shadowOpacity = $0 }
shadow.offset.flatMap { layer.shadowOffset = $0 }
shadow.radius.flatMap { layer.shadowRadius = $0 }
shadow.path.flatMap { layer.shadowPath = $0 }
}
override open func hitTest(_ point: CGPoint, with event: UIEvent!) -> UIView? {