如何在滚动时隐藏并显示带有动画效果的标题视图

问题描述 投票:0回答:3

我正在尝试实现隐藏,并在滚动集合视图时显示像facebook这样的慢动画的标题视图。

图片 :

我的代码:

if (currentContentOffset > self.previousContentOffset) {


    heightConstraintView.constant = 0;
    HeaderView.hidden = YES;

} else if (currentContentOffset < self.previousContentOffset) {

    heightConstraintView.constant = 57;
    HeaderView.hidden = NO;
}
ios objective-c animation uiview uicollectionview
3个回答
1
投票

您可以使用UIView原生动画方法。请注意self.view.layoutIfNeeded是必要的。同样取消隐藏您的观点

heightConstraintView.constant = 57;
UIView.animate(withDuration: 0.25, animations: {
                HeaderView.hidden = NO;
                self.view.layoutIfNeeded()
            })

0
投票

如果你正在使用collectionView,那么我建议使用像View这样的导航栏作为collectionView本身的headerView,这是我如何做的,当我使用需要完成类似的东西时:

使用委托方法:

func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section != 0 {
    return pageControl.numberOfPages
    }
    return 0 //so no cells are displayed for this header, remember to do the same for sizeForItemAtIndex and CellForItemAtIndex too
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    if indexPath.section == 0{
        //add your custom navigation bar like View here
    }
      return UICollectionReusableView()
}

在这种方法中,滚动时标题视图的动画将由集合视图本身处理,并且只有在您向上移动时才会再次显示。


0
投票

如果你使用navigationBar,很容易实现。 iOS为UINavigationController提供了一个掩盖一些复杂行为的简单属性。如果你为任何hidesBarsOnSwipe设置trueUINavigationController,那么iOS会自动为你的视图添加一个轻击手势识别器,以根据需要处理隐藏(和显示)导航栏。这意味着您可以在viewDidAppear中的一行代码中模仿Safari的导航栏行为,如下所示:

self.navigationController?.hidesBarsOnTap = true
© www.soinside.com 2019 - 2024. All rights reserved.