TableView单元格沿y轴的缩放比例不均等

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

我有一个tableView单元格,每个单元格中都有一种颜色。我希望当用户点击单元格时,它能 "打开 "并扩展,使该颜色充满整个屏幕。目前,它只从我点击的单元格开始向下缩放。我也需要它沿着y轴向上缩放,每个单元格都扩展到屏幕的顶部,但我不知道是什么禁止它这样做。

let expandedColorView: UIView = {
        let view = UIView()
        return view
    }()

@objc func userTap(sender: UITapGestureRecognizer) {

        if sender.state == UIGestureRecognizer.State.ended {
            let tapLocation = sender.location(in: self.paletteTableView)

            if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
                if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) {
                    UIView.animate(withDuration: 1.0, animations: {
                        tappedCell.transform = CGAffineTransform(scaleX: 1, y: 50)
                    } )

                }
            }
        }
    }

UITapGestureRecognizer被声明在 tableView(cellForRowAt:)cell.isUserInteractionEnabled = true 启用。

我试着改变了expandedColorView bounds的功能。self.expandedColorView.bounds.size.height = UIScreen.main.bounds.heightUIView.animate 但这并不能改变什么。我想单元格的框架需要改变,使其与父视图框架相匹配(我认为应该是tableView),但我不知道如何做到这一点。

任何帮助将被感激!

我已经附上了问题的gif图。

enter image description here

ios swift uitableview cgaffinetransform
1个回答
0
投票

enter image description here

如果这是你想要的

这是我在数据源扩展中所做的工作。

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = self.tableView.dequeueReusableCell(withIdentifier: "colorCell") as? ColorFulTableViewCell {
            let color = colors[Int(indexPath.row % 7)]
            cell.backgroundColor = color
            return cell
        }
        return UITableViewCell()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if let row = expandCell?.row, row == indexPath.row {
            return self.tableView.bounds.size.height
        }
        else {
            return 100
        }
    }
}

而tableView委托扩展看起来像

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.expandCell == indexPath { return }
        else {
            self.expandCell = indexPath
        }
        self.tableView.reloadRows(at: [indexPath], with: .automatic)
        self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }
}

她怎么了?

heightForRowAt 方法,检查单元格是否需要覆盖整个tableView的大小。 if let row = expandCell?.row, row == indexPath.row { 并设置其高度,通过返回 self.tableView.bounds.size.height 否则我返回100

didSelectRowAt 我将单元格的indexPath更新为展开,保存在 expandCell 然后我重新加载行(这样当这次调用行的高度时,它就可以回到 self.tableView.bounds.size.height 我也叫 scrollToRow(at 担任 .top 以确保我的单元格滚动到顶部,使其完全可见

因为你只重载了一个特定的单元格,虽然从成本的角度来看,它是有效的,但动画可能看起来很生疏,因为可见indexPath数组中的其他单元格正在突然调整它们自己,你总是可以调用重载数据来获得更好的平滑体验。

enter image description here

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.expandCell == indexPath { return }
        else {
            self.expandCell = indexPath
        }
        self.tableView.reloadData()
        self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }
}

希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.