mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Move Animations and UITableView extensions to UIKitExtras.
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
//
|
||||
// Animations.swift
|
||||
// NetNewsWire-iOS
|
||||
//
|
||||
// Created by Maurice Parker on 1/27/20.
|
||||
// Copyright © 2020 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Used to select which animations should be performed
|
||||
public struct Animations: OptionSet, Sendable {
|
||||
|
||||
/// Select and deslections will be animated.
|
||||
public static let select = Animations(rawValue: 1)
|
||||
|
||||
/// Scrolling will be animated
|
||||
public static let scroll = Animations(rawValue: 2)
|
||||
|
||||
/// Pushing and popping navigation view controllers will be animated
|
||||
public static let navigation = Animations(rawValue: 4)
|
||||
|
||||
public let rawValue: Int
|
||||
public init(rawValue: Int) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
//
|
||||
// UITableView-Extensions.swift
|
||||
// RSCoreiOS
|
||||
//
|
||||
// Created by Maurice Parker on 9/6/19.
|
||||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UITableView {
|
||||
|
||||
/**
|
||||
Selects a row and scrolls it to the middle if it is not visible
|
||||
*/
|
||||
public func selectRowAndScrollIfNotVisible(at indexPath: IndexPath, animations: Animations) {
|
||||
guard let dataSource = dataSource,
|
||||
let numberOfSections = dataSource.numberOfSections,
|
||||
indexPath.section < numberOfSections(self),
|
||||
indexPath.row < dataSource.tableView(self, numberOfRowsInSection: indexPath.section) else {
|
||||
return
|
||||
}
|
||||
|
||||
selectRow(at: indexPath, animated: animations.contains(.select), scrollPosition: .none)
|
||||
|
||||
if let visibleIndexPaths = indexPathsForRows(in: safeAreaLayoutGuide.layoutFrame) {
|
||||
if !(visibleIndexPaths.contains(indexPath) && cellCompletelyVisible(indexPath)) {
|
||||
scrollToRow(at: indexPath, at: .middle, animated: animations.contains(.scroll))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cellCompletelyVisible(_ indexPath: IndexPath) -> Bool {
|
||||
let rect = rectForRow(at: indexPath)
|
||||
return safeAreaLayoutGuide.layoutFrame.contains(rect)
|
||||
}
|
||||
|
||||
public func middleVisibleRow() -> IndexPath? {
|
||||
if let visibleIndexPaths = indexPathsForRows(in: safeAreaLayoutGuide.layoutFrame), visibleIndexPaths.count > 2 {
|
||||
return visibleIndexPaths[visibleIndexPaths.count / 2]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user