fix: pr issue

This commit is contained in:
Boyko Mihail 2021-06-28 17:28:09 +03:00
parent 6acf0f0b0f
commit ce426d1f49
11 changed files with 99 additions and 159 deletions

View File

@ -1,37 +1,24 @@
import UIKit
final public class ParalaxAnimator: CollapsibleViewsAnimator {
public var fractionComplete: CGFloat = 0
var isWithAlphaAnimate = true
init(isWithAlphaAnimate: Bool = true) {
self.isWithAlphaAnimate = isWithAlphaAnimate
}
public func setupView(holder: CollapsibleViewsHolder, container: CollapsibleViewsContainer ) {
guard let largeHeaderView = container.bottomHeaderView else {
return
}
holder.navBar?.topItem?.titleView?.alpha = 0
let tableHeaderView = ParallaxTableHeaderView(wrappedView: largeHeaderView)
holder.tableView.tableHeaderView = tableHeaderView
}
public func animate(holder: CollapsibleViewsHolder) {
paralax(holder: holder)
if isWithAlphaAnimate {
holder.navBar?.topItem?.titleView?.alpha = fractionComplete == 1 ? 1 : 0
public var fractionComplete: CGFloat = 0 {
didSet {
navBar?.topItem?.titleView?.alpha = fractionComplete == 1 ? 1 : 0
}
}
private func paralax(holder: CollapsibleViewsHolder) {
guard let header = holder.tableView.tableHeaderView as? ParallaxTableHeaderView else {
return
public var currentContentOffset: CGPoint {
didSet {
tableHeaderView?.layout(for: currentContentOffset)
}
header.layout(for: holder.tableView.contentOffset)
}
private weak var navBar: UINavigationBar?
private weak var tableHeaderView: ParallaxTableHeaderView?
public init(tableHeaderView: ParallaxTableHeaderView, navBar: UINavigationBar? = nil, currentContentOffset: CGPoint) { // if nil - no alpha animation
self.currentContentOffset = currentContentOffset
self.tableHeaderView = tableHeaderView
self.navBar = navBar
}
}

View File

@ -1,8 +1,8 @@
import UIKit
final public class ParalaxWithScaleAnimator: CollapsibleViewsAnimator {
private let paralaxAnimator = ParalaxAnimator(isWithAlphaAnimate: false)
private let scaleAnimator = ScaleAnimator()
private let paralaxAnimator: ParalaxAnimator
private let scaleAnimator: ScaleAnimator
public var fractionComplete: CGFloat = 0 {
didSet {
@ -10,14 +10,20 @@ final public class ParalaxWithScaleAnimator: CollapsibleViewsAnimator {
scaleAnimator.fractionComplete = fractionComplete
}
}
public func setupView(holder: CollapsibleViewsHolder, container: CollapsibleViewsContainer) {
paralaxAnimator.setupView(holder: holder, container: container)
scaleAnimator.setupView(holder: holder, container: container)
public var currentContentOffset: CGPoint {
didSet {
paralaxAnimator.currentContentOffset = currentContentOffset
scaleAnimator.currentContentOffset = currentContentOffset
}
}
public func animate(holder: CollapsibleViewsHolder) {
paralaxAnimator.animate(holder: holder)
scaleAnimator.animate(holder: holder)
private weak var navBar: UINavigationBar?
public init(tableHeaderView: ParallaxTableHeaderView, navBar: UINavigationBar? = nil, currentContentOffset: CGPoint) {
self.navBar = navBar
paralaxAnimator = ParalaxAnimator(tableHeaderView: tableHeaderView, navBar: nil, currentContentOffset: currentContentOffset)
scaleAnimator = ScaleAnimator(navBar: navBar)
self.currentContentOffset = currentContentOffset
}
}

View File

@ -1,8 +1,8 @@
import UIKit
final public class ParalaxWithTransitionAnimator: CollapsibleViewsAnimator {
private let paralaxAnimator = ParalaxAnimator(isWithAlphaAnimate: false)
private let transitionAnimator = TransitionAnimator()
private let paralaxAnimator: ParalaxAnimator
private let transitionAnimator: TransitionAnimator
public var fractionComplete: CGFloat = 0 {
didSet {
@ -10,14 +10,20 @@ final public class ParalaxWithTransitionAnimator: CollapsibleViewsAnimator {
transitionAnimator.fractionComplete = fractionComplete
}
}
public func setupView(holder: CollapsibleViewsHolder, container: CollapsibleViewsContainer) {
paralaxAnimator.setupView(holder: holder, container: container)
transitionAnimator.setupView(holder: holder, container: container)
public var currentContentOffset: CGPoint {
didSet {
paralaxAnimator.currentContentOffset = currentContentOffset
transitionAnimator.currentContentOffset = currentContentOffset
}
}
public func animate(holder: CollapsibleViewsHolder) {
paralaxAnimator.animate(holder: holder)
transitionAnimator.animate(holder: holder)
private weak var navBar: UINavigationBar?
public init(tableHeaderView: ParallaxTableHeaderView, navBar: UINavigationBar? = nil, currentContentOffset: CGPoint) {
self.navBar = navBar
paralaxAnimator = ParalaxAnimator(tableHeaderView: tableHeaderView, navBar: nil, currentContentOffset: currentContentOffset)
transitionAnimator = TransitionAnimator(navBar: navBar)
self.currentContentOffset = currentContentOffset
}
}

View File

@ -1,14 +1,19 @@
import UIKit
final public class ScaleAnimator: CollapsibleViewsAnimator {
public var fractionComplete: CGFloat = 0
public func setupView(holder: CollapsibleViewsHolder, container: CollapsibleViewsContainer) {
holder.navBar?.topItem?.titleView?.alpha = 0
holder.navBar?.topItem?.titleView?.transform = CGAffineTransform(scaleX: -0.5, y: 0.5)
public var fractionComplete: CGFloat = 0 {
didSet {
navBar?.topItem?.titleView?.scale(to: fractionComplete)
}
}
public func animate(holder: CollapsibleViewsHolder) {
holder.navBar?.topItem?.titleView?.scale(to: fractionComplete)
public var currentContentOffset = CGPoint.zero
private weak var navBar: UINavigationBar?
public init(navBar: UINavigationBar? = nil) {
self.navBar = navBar
navBar?.topItem?.titleView?.alpha = 0
navBar?.topItem?.titleView?.transform = CGAffineTransform(scaleX: -0.5, y: 0.5)
}
}

View File

@ -1,13 +1,18 @@
import UIKit
final public class TransitionAnimator: CollapsibleViewsAnimator {
public var fractionComplete: CGFloat = 0
public func setupView(holder: CollapsibleViewsHolder, container: CollapsibleViewsContainer) {
holder.navBar?.topItem?.titleView?.alpha = 0
public var fractionComplete: CGFloat = 0 {
didSet {
navBar?.topItem?.titleView?.transition(to: fractionComplete)
}
}
public func animate(holder: CollapsibleViewsHolder) {
holder.navBar?.topItem?.titleView?.transition(to: fractionComplete)
public var currentContentOffset = CGPoint.zero
private weak var navBar: UINavigationBar?
public init(navBar: UINavigationBar? = nil) {
navBar?.topItem?.titleView?.alpha = 0
self.navBar = navBar
}
}

View File

@ -1,14 +1,14 @@
import UIKit
extension UIView {
public func transition(to coefficient: CGFloat) {
public extension UIView {
func transition(to coefficient: CGFloat) {
UIView.animate(withDuration: 0.2) { [weak self] in
self?.alpha = coefficient
self?.transform = CGAffineTransform(translationX: 0, y: -coefficient*10)
}
}
public func scale(to coefficient: CGFloat) {
func scale(to coefficient: CGFloat) {
UIView.animate(withDuration: 0.2){ [weak self] in
self?.alpha = coefficient
self?.transform = CGAffineTransform(scaleX: coefficient, y: coefficient)

View File

@ -1,10 +1,6 @@
import UIKit
public typealias CollapsibleViewsHolder = TableViewHolder & NavigationBarHolder
public protocol CollapsibleViewsAnimator {
var fractionComplete: CGFloat { get set } // progress of animation
func setupView(holder: CollapsibleViewsHolder, container: CollapsibleViewsContainer)
func animate(holder: CollapsibleViewsHolder)
var fractionComplete: CGFloat { get set } // progress of animation
var currentContentOffset: CGPoint { get set } // offset on content in table view/collection view or plain scroll view
}

View File

@ -1,6 +1,6 @@
import UIKit
public protocol CollapsibleViewsContainer {
public protocol CollapsibleViewsContainer {
var topHeaderView: UIView? { get } // titleView
var bottomHeaderView: UIView? { get } // tableHeaderView

View File

@ -1,9 +1,7 @@
import UIKit
public protocol TransitioningHandler: UIScrollViewDelegate {
var animator: CollapsibleViewsAnimator? { get set }
var animator: CollapsibleViewsAnimator? { get set }
init(collapsibleViewsContainer: CollapsibleViewsContainer?,
collapsibleViewsHolder: CollapsibleViewsHolder?,
animator: CollapsibleViewsAnimator?)
}
init(collapsibleViewsContainer: CollapsibleViewsContainer)
}

View File

@ -1,97 +1,34 @@
import Foundation
import UIKit
final public class TableViewHeaderTransitioningHandler: NSObject, TransitioningHandler {
public var animator: CollapsibleViewsAnimator?
private var collapsibleViewsContainer: CollapsibleViewsContainer?
private var collapsibleViewsHolder: CollapsibleViewsHolder?
final class TableViewHeaderTransitioningHandler: NSObject, TransitioningHandler {
var animator: CollapsibleViewsAnimator?
private var startOffset: CGFloat = 0
private var navigationBarOffset: CGFloat = 0
private var isFirstScroll = true
private var titleView: UIView? {
get {
collapsibleViewsHolder?.navBar?.topItem?.titleView
}
set {
collapsibleViewsHolder?.navBar?.topItem?.titleView = newValue
}
private var container: CollapsibleViewsContainer?
init(collapsibleViewsContainer: CollapsibleViewsContainer) {
self.container = collapsibleViewsContainer
}
private var tableHeaderView: UIView? {
get {
collapsibleViewsHolder?.tableView.tableHeaderView
}
set {
collapsibleViewsHolder?.tableView.tableHeaderView = newValue
}
}
public init(collapsibleViewsContainer: CollapsibleViewsContainer?,
collapsibleViewsHolder: CollapsibleViewsHolder?,
animator: CollapsibleViewsAnimator?) {
self.collapsibleViewsContainer = collapsibleViewsContainer
self.collapsibleViewsHolder = collapsibleViewsHolder
self.animator = animator
super.init()
initialUpdateHeaderView()
}
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let largeHeaderView = collapsibleViewsContainer?.bottomHeaderView else {
titleView?.isHidden = false
guard let largeHeaderView = container?.bottomHeaderView else {
return
}
if isFirstScroll {
startOffset = max(-(collapsibleViewsHolder?.startOffset.y ?? 0), 0)
navigationBarOffset = collapsibleViewsContainer?.fixedTopOffet ?? 0
startOffset = max(-(scrollView.contentOffset.y), 0)
navigationBarOffset = container?.fixedTopOffet ?? 0
isFirstScroll = false
}
let offsetY = scrollView.contentOffset.y + startOffset
var alpha = offsetY / (largeHeaderView.frame.height + navigationBarOffset)
alpha = alpha > 1 ? 1 : alpha
guard let collapsibleViewsHolder = collapsibleViewsHolder,
var animator = animator else {
titleView?.alpha = alpha == 1 ? 1 : 0
return
}
animator.fractionComplete = alpha
animator.animate(holder: collapsibleViewsHolder)
}
private func setupView() {
titleView?.alpha = animator == nil ? 0 : 1
guard let collapsibleViewsHolder = collapsibleViewsHolder,
let collapsibleViewsContainer = collapsibleViewsContainer,
let animator = animator else {
return
}
let alpha = min(offsetY / (largeHeaderView.frame.height + navigationBarOffset), 1)
animator.setupView(holder: collapsibleViewsHolder, container: collapsibleViewsContainer)
}
private func initialUpdateHeaderView() {
titleView = collapsibleViewsContainer?.topHeaderView
setLargeHeader()
setupView()
}
private func setLargeHeader() {
guard let largeHeaderView = collapsibleViewsContainer?.bottomHeaderView else {
return
}
tableHeaderView = largeHeaderView
animator?.fractionComplete = alpha
animator?.currentContentOffset = scrollView.contentOffset
}
}