ios - 快速滚动时自定义标题内容动画

标签 ios swift uitableview

我已经创建了自定义标题,但我不知道如何在滚动时设置动画。

请检查下图,让我知道如何在滚动时设置动画。 Fotmob 中的动画标题应用程序。

enter image description here

最佳答案

首先添加 header view 作为 UIvVew 并在 headerView 下方添加 UIScrollView 或 UITableView 与屏幕截图相同,然后按照以下步骤操作。

enter image description here

  1. 为标题 View 设置固定高度约束(例如 125)并将其附加到顶部、左侧和右侧。

  2. 让下面的 UIScrollView 使用所有可用空间,因此将顶部、底部、左侧和右侧约束设置为零。

  3. 将页眉 View 高度约束连接到 ViewController 以实现类似以下内容:

     @IBOutlet var headerViewHeightConstraint: NSLayoutConstraint!
    
  4. 将 UIScrollView 委托(delegate)设置为 ViewController

  5. 声明两个属性来限制标题 View 的最大高度和最小高度,fox 示例:

    let headerViewMaxHeight: CGFloat = 125
    let headerViewMinHeight: CGFloat = 44 + UIApplication.shared.statusBarFrame.height
    
  6. 整个解决方法基于在 UIScrollView 滚动时更新标题 View 高度约束,因此让我们实现 UIScrollViewDelegate 和我们案例中最重要的委托(delegate),scrollViewDidScroll:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        let headerViewMinHeight: CGFloat = 44 + UIApplication.shared.statusBarFrame.height
    
        let yPos = mainScrollView.contentOffset.y
        let newHeaderViewHeight: CGFloat = headerViewHeightConstraint.constant - yPos
    
        if newHeaderViewHeight > headerViewMaxHeight {
            // Here, Manage Your Score Format View
            headerViewHeightConstraint.constant = max(headerViewMaxHeight, newHeaderViewHeight)
    
        } else if newHeaderViewHeight < headerViewMinHeight {
    
        headerViewHeightConstraint.constant = headerViewMinHeight
    
        } else {
    
            headerViewHeightConstraint.constant = newHeaderViewHeight
            scrollView.contentOffset.y = 0 // block scroll view
    
        }
    }
    

关于ios - 快速滚动时自定义标题内容动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56371946/

相关文章:

ios - 如果 iPhone 没有越狱,macOS 上的 iTunes 可以检测到 iPhone 吗?

ios - 如何从主视图 Controller 在详细 View Controller 中推送新 View ?

ios - 如何重构我的代码以在主线程上调用 AppDelegate?

ios - 如何防止 subview 在 ScrollView 内缩放

ios - UITableView:如何仅针对单元格将 clipsToBounds 设置为 Yes?

ios - AFNetworking、UITableView 和 block

ios - 在 iOS 应用程序中导入 MP3(使用代码)

ios - Mapbox 注释标注

ios - 在@IBAction 中分配来自不同类的另一个变量?

ios - 按字母顺序将 Tableview 单元格放入部分 - Swift 3 和 Firebase 数据库