【问题标题】:How can i do hide and show the header view with animation effect while scrolling滚动时如何隐藏和显示带有动画效果的标题视图
【发布时间】:2018-02-28 08:00:33
【问题描述】:

我正在尝试在滚动集合视图时使用像 facebook 这样的慢速动画来隐藏和显示标题视图。

图片:

我的代码:

if (currentContentOffset > self.previousContentOffset) {


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

} else if (currentContentOffset < self.previousContentOffset) {

    heightConstraintView.constant = 57;
    HeaderView.hidden = NO;
}

【问题讨论】:

  • 您是否尝试使用self.navigationController?.hidesBarsOnSwipe = true 将其添加到 viewDidLoad 并检查是否是您想要的
  • 它不是导航控制器。它只是一个uiview
  • 请将此代码放入目标c。
  • 使用导航控制器代替uiview
  • @geeks-of-geeks : 对不起我的错误不要使用上面的代码它会添加两个约束而不是修改现有的约束 lemme update

标签: ios objective-c animation uiview uicollectionview


【解决方案1】:

你可以使用UIView原生动画方法。请注意self.view.layoutIfNeeded 是必需的。对取消隐藏您的视图执行相同操作

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

【讨论】:

    【解决方案2】:

    如果您使用的是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()
    }
    

    在这种方法中,当你滚动时,标题视图的动画将由集合视图本身处理,只有当你到达顶部时它才会再次出现。

    【讨论】:

      【解决方案3】:

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

      self.navigationController?.hidesBarsOnTap = true
      

      【讨论】:

      • 但我只在视图上实现这是最大的问题。
      猜你喜欢
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多