gpt4 book ai didi

ios - 如何在快速向上/向下滚动时同时隐藏/显示 View

转载 作者:行者123 更新时间:2023-12-03 08:07:51 25 4
gpt4 key购买 nike

Storyboard设计层次结构如下design hierarchy

这里,如果我向上滚动,我只需要隐藏 topView,并且需要在顶部显示 secondaryView,并且滚动应该从 secondaryView 的底部执行,如果我向下滚动,也需要显示 topView

代码:使用此代码,topView 在向上/向下滚动时立即隐藏/显示。但是当我向上滚动时,topView 应该与向上滚动一起明显(同时)隐藏如何实现。请指导。

class ThirdVC: UIViewController, UNUserNotificationCenterDelegate, UIScrollViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var topView: UIView!
@IBOutlet weak var topviewHeight: NSLayoutConstraint!
@IBOutlet weak var secondViewHeight: NSLayoutConstraint!
@IBOutlet weak var secondViewTopConstraint: NSLayoutConstraint!

override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().delegate = self

scrollView.delegate = self
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {

if scrollView.contentOffset.y > 0{
topviewHeight.constant = 0
UIView.animate(withDuration: 0.2) {
self.view.layoutIfNeeded()
}

}else{
topviewHeight.constant = 100
UIView.animate(withDuration: 0.2) {
self.view.layoutIfNeeded()
}
}
}
}

o/p:

向上滚动之前的初始o/p

initial o/p before scroll-up screen

向上滚动后:立即隐藏,我需要它随着向上滚动而隐藏

after scroll-up screen

编辑:当滚动第二个 View 顶部到达第一个 View 顶部时,我需要,然后第一个 View 必须隐藏

那么如何计算才能达到呢?是否可以使用 secodeView top constarint 值更改?那么如何计算呢?请指导

这里附上所需的o/p屏幕记录

required o/p screen record gif

最佳答案

这个想法是锁定如果动画 block 已经在进行中,则不会触发。

最简单的方法是使用 bool 来跟踪动画的状态

首先,使用一些变量来帮助您跟踪动画和顶 View 的状态

// Persist the top view height constraint
var topViewHeightConstraint: NSLayoutConstraint?

// Original height of the top view
var viewHeight: CGFloat = 100

// Keep track of the
private var isAnimationInProgress = false

然后在执行动画时使用这些变量

extension ScrollViewAnimateVC: UIScrollViewDelegate {

func scrollViewDidScroll(_ scrollView: UIScrollView) {

// Check if the animation is locked or not
if !isAnimationInProgress {

guard let topViewHeightConstraint = topViewHeightConstraint
else { return }

// Check if an animation is required
if scrollView.contentOffset.y > .zero &&
topViewHeightConstraint.constant > .zero {

topViewHeightConstraint.constant = .zero
animateTopViewHeight()
}
else if scrollView.contentOffset.y <= .zero
&& topViewHeightConstraint.constant <= .zero {

topViewHeightConstraint.constant = viewHeight
animateTopViewHeight()
}
}
}

// Animate the top view
private func animateTopViewHeight() {

// Lock the animation functionality
isAnimationInProgress = true

UIView.animate(withDuration: 0.2) {

self.view.layoutIfNeeded()

} completion: { [weak self] (_) in

// Unlock the animation functionality
self?.isAnimationInProgress = false
}
}
}

这会给你一些像这样更流畅的东西

Animate UIView height autolayout to show hide with UIScrollView swift iOS UIView animation block

根据 OP 评论进行更新

我正在尝试使用第二个 View (greenview)顶部约束值根据向上和向下滚动进行更改...但在这里我无法计算隐藏和显示的正确值

如果你想这样做,你实际上不需要动画 block ,你可以不断减少顶 View 高度直到它变成0。

添加这些

// Stores the original offset of the scroll view
var previousOffset: CGPoint?

override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)
previousOffset = scrollView.contentOffset
}

然后更新scrollViewDidScroll

extension ScrollViewAnimateVC: UIScrollViewDelegate {

func scrollViewDidScroll(_ scrollView: UIScrollView) {

guard let topViewHeightConstraint = topViewHeightConstraint
else { return }

let currentOffset = scrollView.contentOffset

if let startOffset = previousOffset {

// Get the distance scrolled
let delta = abs((startOffset.y - currentOffset.y))

if currentOffset.y > startOffset.y,
currentOffset.y > .zero {
// Scrolling down

// Set the new height based on the amount scrolled
var newHeight = topViewHeightConstraint.constant - delta

// Make sure we do not go below 0
if newHeight < .zero {
newHeight = .zero
}

topViewHeightConstraint.constant
= newHeight

}
else if currentOffset.y < startOffset.y,
currentOffset.y <= viewHeight {
// Scrolling up

var newHeight = topViewHeightConstraint.constant + delta

// Make sure we do not go above the max height
if newHeight > viewHeight {
newHeight = viewHeight
}

topViewHeightConstraint.constant
= newHeight
}

// Update the previous offset
previousOffset = scrollView.contentOffset

self.view.layoutIfNeeded()
}
}
}

这会给你这样的东西:

UIScrollView change height of view on scroll swift iOS

关于ios - 如何在快速向上/向下滚动时同时隐藏/显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71629494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com