使用队列而不是递归.
我想你可以这样做……
extension UIView {
func changeColor(color: UIColor) {
var queue = [self]
while let view = queue.first() {
queue += view.subviews
view.backgroundColor = color
queue.remove(view)
}
}
}
我是用队列来做的,而不是递归地做.但该方法不起作用.
做你想要的问题
你可以这样做,这就是你在问题中提出的想法.
extension UIView {
// this name isn't very good but you get the idea
func applyChangeToAllSubviews(block: (UIView) -> ()) {
var queue = [self]
// I don't have my dev computer dos not sure of the syntax
while let view = queue.first() {
queue += view.subviews
block(view)
queue.remove(view)
}
}
}
然后像……一样运行它
someView.applyChangeToAllSubviews {
view in
view.backgroundColor = UIColor.whiteColor()
}
我想这有点功能……是的.