82 lines
1.6 KiB
Swift
82 lines
1.6 KiB
Swift
//
|
|
// DimmedView.swift
|
|
// PanModal
|
|
//
|
|
// Copyright © 2017 Tiny Speck, Inc. All rights reserved.
|
|
//
|
|
|
|
#if os(iOS)
|
|
import UIKit
|
|
|
|
/**
|
|
A dim view for use as an overlay over content you want dimmed.
|
|
*/
|
|
open class DimmedView: UIView {
|
|
|
|
/**
|
|
Represents the possible states of the dimmed view.
|
|
max, off or a percentage of dimAlpha.
|
|
*/
|
|
public enum DimState {
|
|
case max
|
|
case off
|
|
case percent(CGFloat)
|
|
}
|
|
|
|
// MARK: - Properties
|
|
|
|
/**
|
|
The state of the dimmed view
|
|
*/
|
|
var dimState: DimState = .off {
|
|
didSet {
|
|
onChange(dimState: dimState)
|
|
}
|
|
}
|
|
|
|
/**
|
|
The closure to be executed when a tap occurs
|
|
*/
|
|
var didTap: ((_ recognizer: UITapGestureRecognizer) -> Void)?
|
|
|
|
/**
|
|
Tap gesture recognizer
|
|
*/
|
|
private lazy var tapGesture: UIGestureRecognizer = {
|
|
return UITapGestureRecognizer(target: self, action: #selector(didTapView))
|
|
}()
|
|
|
|
// MARK: - Initializers
|
|
|
|
init() {
|
|
super.init(frame: .zero)
|
|
alpha = 0.0
|
|
addGestureRecognizer(tapGesture)
|
|
}
|
|
|
|
required public init?(coder aDecoder: NSCoder) {
|
|
fatalError()
|
|
}
|
|
|
|
// MARK: - Event Handlers
|
|
|
|
@objc private func didTapView(sender: UITapGestureRecognizer) {
|
|
didTap?(sender)
|
|
}
|
|
|
|
// MARK: - Subclass override
|
|
|
|
open func onChange(dimState: DimState) {
|
|
switch dimState {
|
|
case .max:
|
|
alpha = 1.0
|
|
case .off:
|
|
alpha = 0.0
|
|
case .percent(let percentage):
|
|
alpha = max(0.0, min(1.0, percentage))
|
|
}
|
|
}
|
|
|
|
}
|
|
#endif
|