152 lines
6.3 KiB
Swift
152 lines
6.3 KiB
Swift
//
|
|
// Copyright (c) 2018 Loverde Co.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
#if os(iOS) || os(macOS)
|
|
//MARK: - UITableView Animation Cell
|
|
public typealias UITableViewCellAnimation = (UITableViewCell, IndexPath, UITableView) -> Void
|
|
|
|
public class UITableViewAnimator {
|
|
private let animation: UITableViewCellAnimation
|
|
|
|
public init(animation: @escaping UITableViewCellAnimation) {
|
|
self.animation = animation
|
|
}
|
|
|
|
public func animate(cell: UITableViewCell, at indexPath: IndexPath, in tableView: UITableView) {
|
|
animation(cell, indexPath, tableView)
|
|
}
|
|
}
|
|
|
|
public extension UITableView {
|
|
|
|
/// Reload data with a completion handler.
|
|
///
|
|
/// - Parameter completion: completion handler to run after reloadData finishes.
|
|
func reloadData(_ completion: @escaping () -> Void) {
|
|
UIView.animate(withDuration: 0, animations: {
|
|
self.reloadData()
|
|
}, completion: { _ in
|
|
completion()
|
|
})
|
|
}
|
|
|
|
/// Loverde Co.: Animate cell to up with fade - See file example to know how to use
|
|
///
|
|
func makeMoveUpWithFadeAnimation(rowHeight: CGFloat, duration: TimeInterval, delayFactor: TimeInterval) -> UITableViewCellAnimation {
|
|
return { cell, indexPath, _ in
|
|
cell.transform = CGAffineTransform(translationX: 0, y: rowHeight * 1.4)
|
|
cell.alpha = 0
|
|
UIView.animate(
|
|
withDuration: duration,
|
|
delay: delayFactor * Double(indexPath.row),
|
|
options: [.curveEaseInOut],
|
|
animations: {
|
|
cell.transform = CGAffineTransform(translationX: 0, y: 0)
|
|
cell.alpha = 1
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Dequeue reusable UITableViewCell using class name
|
|
///
|
|
/// - Parameter name: UITableViewCell type
|
|
/// - Returns: UITableViewCell object with associated class name.
|
|
func dequeueReusableCell<T: UITableViewCell>(withClass name: T.Type) -> T {
|
|
guard let cell = dequeueReusableCell(withIdentifier: String(describing: name)) as? T else {
|
|
fatalError("Couldn't find UITableViewCell for \(String(describing: name)), make sure the cell is registered with table view")
|
|
}
|
|
return cell
|
|
}
|
|
|
|
/// Dequeue reusable UITableViewCell using class name for indexPath
|
|
///
|
|
/// - Parameters:
|
|
/// - name: UITableViewCell type.
|
|
/// - indexPath: location of cell in tableView.
|
|
/// - Returns: UITableViewCell object with associated class name.
|
|
func dequeueReusableCell<T: UITableViewCell>(withClass name: T.Type, for indexPath: IndexPath) -> T {
|
|
guard let cell = dequeueReusableCell(withIdentifier: String(describing: name), for: indexPath) as? T else {
|
|
fatalError("Couldn't find UITableViewCell for \(String(describing: name)), make sure the cell is registered with table view")
|
|
}
|
|
return cell
|
|
}
|
|
|
|
/// Dequeue reusable UITableViewHeaderFooterView using class name
|
|
///
|
|
/// - Parameter name: UITableViewHeaderFooterView type
|
|
/// - Returns: UITableViewHeaderFooterView object with associated class name.
|
|
func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(withClass name: T.Type) -> T {
|
|
guard let headerFooterView = dequeueReusableHeaderFooterView(withIdentifier: String(describing: name)) as? T else {
|
|
fatalError("Couldn't find UITableViewHeaderFooterView for \(String(describing: name)), make sure the view is registered with table view")
|
|
}
|
|
return headerFooterView
|
|
}
|
|
|
|
func dequeueCell<T: UITableViewCell>(indexPath: IndexPath) -> T {
|
|
guard let cell = self.dequeueReusableCell(withIdentifier: T.identifier, for: indexPath) as? T else {
|
|
fatalError("Couldn't find UITableViewCell for \(String(describing: T.className)), make sure the view is registered with table view")
|
|
}
|
|
return cell
|
|
}
|
|
|
|
/// Check whether IndexPath is valid within the tableView
|
|
///
|
|
/// - Parameter indexPath: An IndexPath to check
|
|
/// - Returns: Boolean value for valid or invalid IndexPath
|
|
func isValidIndexPath(_ indexPath: IndexPath) -> Bool {
|
|
return indexPath.section >= 0 &&
|
|
indexPath.row >= 0 &&
|
|
indexPath.section < numberOfSections &&
|
|
indexPath.row < numberOfRows(inSection: indexPath.section)
|
|
}
|
|
|
|
/// Safely scroll to possibly invalid IndexPath
|
|
///
|
|
/// - Parameters:
|
|
/// - indexPath: Target IndexPath to scroll to
|
|
/// - scrollPosition: Scroll position
|
|
/// - animated: Whether to animate or not
|
|
func safeScrollToRow(at indexPath: IndexPath, at scrollPosition: UITableView.ScrollPosition, animated: Bool) {
|
|
guard indexPath.section < numberOfSections else { return }
|
|
guard indexPath.row < numberOfRows(inSection: indexPath.section) else { return }
|
|
scrollToRow(at: indexPath, at: scrollPosition, animated: animated)
|
|
}
|
|
}
|
|
|
|
extension UITableViewCell {
|
|
|
|
public static var identifier: String {
|
|
return "id"+String(describing: self)
|
|
}
|
|
|
|
public func prepareDisclosureIndicator() {
|
|
for case let button as UIButton in subviews {
|
|
let image = button.backgroundImage(for: .normal)?.withRenderingMode(.alwaysTemplate)
|
|
button.setBackgroundImage(image, for: .normal)
|
|
}
|
|
}
|
|
}
|
|
#endif
|