132 lines
2.9 KiB
Swift
132 lines
2.9 KiB
Swift
//
|
|
// DimmedView.swift
|
|
// PanModal
|
|
//
|
|
// Copyright © 2017 Tiny Speck, Inc. All rights reserved.
|
|
//
|
|
|
|
#if os(iOS)
|
|
import TISwiftUtils
|
|
import TIUIKitCore
|
|
import TIUIElements
|
|
import UIKit
|
|
|
|
/**
|
|
A dim view for use as an overlay over content you want dimmed.
|
|
*/
|
|
public class DimmedView: BaseInitializableView {
|
|
|
|
public enum AppearanceType {
|
|
|
|
case transparent
|
|
case transparentWithShadow(UIViewShadow)
|
|
case opaque
|
|
|
|
var isTransparent: Bool {
|
|
switch self {
|
|
case .transparent, .transparentWithShadow:
|
|
return true
|
|
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
Represents the possible states of the dimmed view.
|
|
max, off or a percentage of dimAlpha.
|
|
*/
|
|
enum DimState {
|
|
case max
|
|
case off
|
|
case percent(CGFloat)
|
|
}
|
|
|
|
// MARK: - Properties
|
|
|
|
/**
|
|
The state of the dimmed view
|
|
*/
|
|
var dimState: DimState = .off {
|
|
didSet {
|
|
guard !appearanceType.isTransparent else {
|
|
alpha = .zero
|
|
return
|
|
}
|
|
|
|
switch dimState {
|
|
case .max:
|
|
alpha = 1.0
|
|
case .off:
|
|
alpha = 0.0
|
|
case .percent(let percentage):
|
|
alpha = max(0.0, min(1.0, percentage))
|
|
}
|
|
}
|
|
}
|
|
|
|
weak var presentingController: UIViewController?
|
|
var appearanceType: AppearanceType
|
|
|
|
/**
|
|
The closure to be executed when a tap occurs
|
|
*/
|
|
var didTap: VoidClosure?
|
|
|
|
// MARK: - Initializers
|
|
|
|
init(presentingController: UIViewController? = nil,
|
|
dimColor: UIColor = UIColor.black.withAlphaComponent(0.7),
|
|
appearanceType: AppearanceType) {
|
|
|
|
self.presentingController = presentingController
|
|
self.appearanceType = appearanceType
|
|
|
|
super.init(frame: .zero)
|
|
|
|
alpha = 0.0
|
|
backgroundColor = dimColor
|
|
}
|
|
|
|
required public init?(coder aDecoder: NSCoder) {
|
|
fatalError()
|
|
}
|
|
|
|
// MARK: - BaseInitializableView
|
|
|
|
public override func bindViews() {
|
|
super.bindViews()
|
|
|
|
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapView))
|
|
addGestureRecognizer(tapGesture)
|
|
}
|
|
|
|
// MARK: - Overrides
|
|
|
|
public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
|
|
if appearanceType.isTransparent {
|
|
let subviews = presentingController?.view.subviews.reversed() ?? []
|
|
|
|
for subview in subviews {
|
|
if let hittedView = subview.hitTest(point, with: event) {
|
|
return hittedView
|
|
}
|
|
}
|
|
}
|
|
|
|
return super.hitTest(point, with: event)
|
|
}
|
|
|
|
public override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
|
!appearanceType.isTransparent
|
|
}
|
|
|
|
// MARK: - Event Handlers
|
|
|
|
@objc private func didTapView() {
|
|
didTap?()
|
|
}
|
|
}
|
|
#endif
|