Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent view from expanding accidentally #12

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,22 @@
</Actions>
<Locations>
<Location
uuid = "C9CE3BF8-7173-4116-B160-3D92C310E179 - ca8a62ba89de0751"
uuid = "C9CE3BF8-7173-4116-B160-3D92C310E179 - dc2f8c5e001177f0"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "UIKit.UIApplicationMain(Swift.Int32, Swift.Optional&lt;Swift.UnsafeMutablePointer&lt;Swift.UnsafeMutablePointer&lt;Swift.Int8&gt;&gt;&gt;, Swift.Optional&lt;Swift.String&gt;, Swift.Optional&lt;Swift.String&gt;) -&gt; Swift.Int32"
moduleName = "libswiftUIKit.dylib"
usesParentBreakpointCondition = "Yes"
offsetFromSymbolStart = "0">
symbolName = "UIApplicationMain"
moduleName = "UIKitCore"
usesParentBreakpointCondition = "Yes">
</Location>
<Location
uuid = "C9CE3BF8-7173-4116-B160-3D92C310E179 - dc2f8c5e001177f0"
uuid = "C9CE3BF8-7173-4116-B160-3D92C310E179 - ed0225d50b1c0976"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "UIApplicationMain"
symbolName = "UIKit.UIApplicationMain(Swift.Int32, Swift.Optional&lt;Swift.UnsafeMutablePointer&lt;Swift.UnsafeMutablePointer&lt;Swift.Int8&gt;&gt;&gt;, Swift.Optional&lt;Swift.String&gt;, Swift.Optional&lt;Swift.String&gt;) -&gt; Swift.Int32"
moduleName = "UIKitCore"
usesParentBreakpointCondition = "Yes"
offsetFromSymbolStart = "0">
usesParentBreakpointCondition = "Yes">
</Location>
</Locations>
</BreakpointContent>
Expand Down
60 changes: 30 additions & 30 deletions ScrollEdgeControl/Core/ScrollStickyVerticalHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ extension ScrollStickyContentType {
public final class ScrollStickyVerticalHeaderView: UIView {

public struct Configuration: Equatable {

public var scrollsTogether: Bool
public var attachesToSafeArea: Bool

public init(scrollsTogether: Bool = true, attachesToSafeArea: Bool = false) {
self.scrollsTogether = scrollsTogether
self.attachesToSafeArea = attachesToSafeArea
Expand All @@ -51,21 +51,21 @@ public final class ScrollStickyVerticalHeaderView: UIView {
var safeAreaInsets: UIEdgeInsets = .zero
var contentOffset: CGPoint = .zero
var isActive: Bool = true

var configuration: Configuration
}

public var configuration: Configuration {
get { componentState.configuration }
set { componentState.configuration = newValue }
}

public var isActive: Bool {
componentState.isActive
}

private var isInAnimating = false

private var componentState: ComponentState {
didSet {
guard oldValue != componentState else {
Expand All @@ -85,17 +85,17 @@ public final class ScrollStickyVerticalHeaderView: UIView {
}

private var contentView: ScrollStickyContentType?

private var observations: [NSKeyValueObservation] = []

private var contentInsetTopDynamicAnimator: Animator<CGFloat>?

private var topConstraint: NSLayoutConstraint?

private weak var targetScrollView: UIScrollView? = nil

public init(configuration: Configuration = .init()) {

self.componentState = .init(configuration: configuration)
super.init(frame: .null)
}
Expand All @@ -114,20 +114,19 @@ public final class ScrollStickyVerticalHeaderView: UIView {
}

addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: topAnchor),
contentView.leftAnchor.constraint(equalTo: leftAnchor),
contentView.rightAnchor.constraint(equalTo: rightAnchor),
contentView.bottomAnchor.constraint(equalTo: bottomAnchor),
])

reloadState(animated: false)

contentView.receive(state: contentState, oldState: nil)
}

public override func layoutSubviews() {
super.layoutSubviews()

// sync frame
// we don't use AutoLayout to prevent the content view from expanding inside.
contentView?.frame = bounds
}

public func setIsActive(_ isActive: Bool, animated: Bool) {

if animated {
Expand All @@ -143,7 +142,7 @@ public final class ScrollStickyVerticalHeaderView: UIView {
public override func didMoveToSuperview() {

super.didMoveToSuperview()

// We cannot rely on the existence of self.superview to decide whether to setup scrollView,
// because it did not exist yet on iOS 13.
guard componentState.hasAttachedToScrollView == false else {
Expand Down Expand Up @@ -178,7 +177,7 @@ public final class ScrollStickyVerticalHeaderView: UIView {
addObservation(scrollView: scrollView)

self.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
leftAnchor.constraint(equalTo: scrollView.frameLayoutGuide.leftAnchor),
rightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.rightAnchor),
Expand Down Expand Up @@ -211,7 +210,7 @@ public final class ScrollStickyVerticalHeaderView: UIView {
else {
return
}

self.componentState.contentOffset = scrollView.contentOffset
self.contentState.contentOffset = scrollView.contentOffset
},
Expand Down Expand Up @@ -259,9 +258,9 @@ public final class ScrollStickyVerticalHeaderView: UIView {
if state.isActive != oldState?.isActive {
contentState.isActive = state.isActive
}

if let targetScrollView = targetScrollView, state.configuration.attachesToSafeArea != oldState?.configuration.attachesToSafeArea {

if state.configuration.attachesToSafeArea {
topConstraint?.isActive = false
topConstraint = topAnchor.constraint(equalTo: targetScrollView.safeAreaLayoutGuide.topAnchor)
Expand All @@ -271,21 +270,21 @@ public final class ScrollStickyVerticalHeaderView: UIView {
topConstraint = topAnchor.constraint(equalTo: targetScrollView.frameLayoutGuide.topAnchor)
topConstraint?.isActive = true
}

layoutIfNeeded()

}

if let targetScrollView = targetScrollView,
let contentView = contentView,
let contentView = contentView,
state.safeAreaInsets != oldState?.safeAreaInsets || state.isActive != oldState?.isActive || state.configuration != oldState?.configuration
{

let targetValue: CGFloat = {
if state.isActive {

let size = calculateFittingSize(view: contentView)

if state.configuration.attachesToSafeArea {
let targetValue = size.height
return targetValue
Expand Down Expand Up @@ -338,13 +337,14 @@ public final class ScrollStickyVerticalHeaderView: UIView {

}
}

if let topConstraint = topConstraint, state.contentOffset != oldState?.contentOffset || state.configuration != oldState?.configuration {
if self.configuration.scrollsTogether {
topConstraint.constant = min(0, -(state.contentOffset.y + (targetScrollView?.adjustedContentInset.top ?? 0)))
}

}

}

private func calculateFittingSize(view: UIView) -> CGSize {
Expand Down
Loading