【问题标题】:Scroll a UIImageView on top of UITableView automatically with UITableView使用 UITableView 在 UITableView 上自动滚动 UIImageView
【发布时间】:2017-12-17 17:50:13
【问题描述】:

我有一个屏幕,上面有一个 UIImageView,下面有一个 UITableView -

我想在表格视图滚动时上下移动图像视图,以便表格视图最终可以占据整个屏幕,然后在表格视图内容一直向后滚动时回到原来的位置到达顶点。到目前为止,我实现了这一点-

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print("scrolled")
    print(scrollView.contentOffset)

    let currentOffset = scrollView.contentOffset.y

    let netOffset = previousOffset + currentOffset

    self.previousOffset = currentOffset

    if netOffset > 0 {
        self.imageViewTopConstraint.constant = -netOffset
    } else if netOffset < 0 {
        self.imageViewTopConstraint.constant = netOffset
    } else {
        self.imageViewTopConstraint.constant = self.imageViewOriginalTop
        self.previousOffset = 0.0
    }

}

上面的代码似乎工作正常,但是当表格视图一直向下滚动时,单元格似乎闪烁(可能是因为滚动事件在表格视图内容全部滚动时由于弹跳效应不断传递向上还是向下?)

我该如何解决这个问题?

【问题讨论】:

    标签: ios uitableview uiscrollview uiimageview xcode9.2


    【解决方案1】:

    您可以通过编程方式将 UIImageView 添加为 UITableViewHeader

    @IBOutlet weak var tableView: UITableView!
    
    let imageView = UIImageView(image: UIImage(named: "myImage.png"))
    imageView.contentMode = .scaleAspectFit
    tableView.tableHeaderView = imageView
    

    【讨论】:

    • 这种方法为我提供了没有所有滚动和动态约束复杂性的解决方案。谢谢!
    猜你喜欢
    • 2012-12-27
    • 2019-06-12
    • 2014-09-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多