Swift - 如何以编程方式在 UITableViewHeader 中旋转 UIImageView

Swift - How to programmatically rotate a UIImageView in UITableViewHeader

我构建了一个带有可折叠部分的 UITableView,使用我编写的代码,我可以设法更改 header 中按钮的标题。 然后我决定我想要在右边的一个UIImageView中有一个旋转的箭头,所以我建了它。

这是我构建界面的方式:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    // Create a UIView with title and button inside
    let myView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 36))
    myView.backgroundColor = .red

    let button = UIButton(type: .system)
    button.addTarget(self, action: #selector(openClose), for: .touchUpInside)
    button.tag = section
    button.translatesAutoresizingMaskIntoConstraints = false

    let myImageVIew: UIImageView = {
       let iv = UIImageView()
        iv.image = UIImage(named: "HeaderImage")
        iv.translatesAutoresizingMaskIntoConstraints = false
        return iv
    }()

    myView.addSubview(myImageVIew)
    myImageVIew.rightAnchor.constraint(equalTo: myView.rightAnchor).isActive = true
    myImageVIew.centerYAnchor.constraint(equalTo: myView.centerYAnchor).isActive = true
    myImageVIew.heightAnchor.constraint(equalTo: myView.heightAnchor).isActive = true
    myImageVIew.widthAnchor.constraint(equalTo: myView.heightAnchor).isActive = true

    myView.addSubview(button)
    button.centerYAnchor.constraint(equalTo: myView.centerYAnchor).isActive = true
    button.rightAnchor.constraint(equalTo: myView.rightAnchor).isActive = true
    button.widthAnchor.constraint(equalTo: myView.widthAnchor).isActive = true
    button.heightAnchor.constraint(equalTo: myView.heightAnchor).isActive = true

    return myView
}

下面是我open/close这些部分的方式:

@objc
private func openClose(button: UIButton) {
    let section = button.tag
    var indexPaths = [IndexPath]()
    for row in storedData[section].names.indices {
        let indexPath = IndexPath(row: row, section: section)
        indexPaths.append(indexPath)
    }

    let isExpanded = storedData[section].isExpanded
    storedData[section].isExpanded = !isExpanded

    if isExpanded {
        // Here the code to rotate the image
    } else {
        // Here the code to rotate the image
    }

    if !isExpanded {
        tableView.insertRows(at: indexPaths, with: .fade)
    } else {
        tableView.deleteRows(at: indexPaths, with: .fade)
    }
}

有没有办法从 openClose() 函数访问 UIImageView? 如果没有,我该如何更正此代码?

这里还有一个允许一切正常工作的结构:

struct ExpandableNames {
var isExpanded: Bool
let names: [String]
}

谢谢。

一种干净的方法是将 UIView 子类化(您可能还想查看 UITableViewHeaderFooterView,如果您遇到性能问题,它是可重用的)。然后使用布局驱动 UI 动画 - 参见 https://developer.apple.com/videos/play/wwdc2018/233/

要从您的 openClose 操作访问 header 视图,您可以查询委托方法 tableView(_:viewForHeaderInSection:)。您可能稍后可以更好地构建它,使用标记 属性 似乎不是很可扩展,但您可以稍后重构。

以下是我如何做到的: 我将此代码添加到我的 openClose() 函数中。

    let section = sender.tag
    let subs = sender.superview?.subviews

    let image = subs?[0] as! UIImageView
    image.image = UIImage(named: "HeaderImage")

这允许我使用普通的 CGAffineTransform 旋转图像。