简体   繁体   English

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

[英]Swift - How to programmatically rotate a UIImageView in UITableViewHeader

I have built a UITableView with collapsable sections and with the code I have written I can manage to change the title of the button in the header. 我建立了一个具有可折叠部分的UITableView,并使用我编写的代码可以更改标题中按钮的标题。 Then I decided that I want a rotating arrow in a UIImageView on the right, so I built it. 然后,我决定要在右侧的UIImageView中使用旋转箭头,因此构建了它。

Here's how I built the interface: 这是我构建界面的方式:

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
}

And here's how I open/close the sections: 这是我打开/关闭部分的方式:

@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)
    }
}

Is there a way to access the UIImageView from the openClose() function? 有没有办法从openClose()函数访问UIImageView? If there isn't, how can I correct this code? 如果没有,我该如何纠正此代码?

Also here's the struct that allows everything to work: 这也是允许所有工作的结构:

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

Thank you. 谢谢。

A clean approach would be to subclass the UIView (you also might want to look into UITableViewHeaderFooterView which is reusable if you have performance problems). 干净的方法是将UIView子类化(您可能还希望查看UITableViewHeaderFooterView,如果您遇到性能问题,可以重用)。 Then use layout driven UI animations - see https://developer.apple.com/videos/play/wwdc2018/233/ 然后使用布局驱动的UI动画-请参阅https://developer.apple.com/videos/play/wwdc2018/233/

To access the header view from your openClose action, you could query the delegate method tableView(_:viewForHeaderInSection:). 要从openClose操作访问标题视图,可以查询委托方法tableView(_:viewForHeaderInSection :)。 You can probably structure this better a bit later, using the tag property doesn't seem very scalable, but you can refactor a little later. 您可能可以稍后再构造一下更好的结构,使用tag属性似乎没有很好的可扩展性,但是您可以稍后再重构。

Here's how I managed to do it: I added into my openClose() function this code. 我设法做到这一点:我将此代码添加到openClose()函数中。

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

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

This allows me to rotate the Image with a normal CGAffineTransform. 这使我可以使用普通的CGAffineTransform旋转图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM