我正在尝试使用数组循环来实现对iOS UI元素的控制。但是,看起来我将更改应用于框架项的位置,它应用于值而不是引用。如果我对UI元素本身进行相同的更改,它就可以工作。
在C中,我会说&someElement
并在处理过程中取消引用它。我如何在Swift中实现同样的目标?
class UIComponentLocationStruct {
var control: CGRect // might be a UILabel, UIButton, or some other item
var xOffset: Int
var yOffset: Int
init( control:CGRect, xOffset:Int, yOffset:Int ){
self.control = control
self.xOffset = xOffset
self.yOffset = yOffset
}
}
class rowOfComponents {
var yOffset: Int
var components: [UIComponentLocationStruct]
init( yOffset:Int, components:[UIComponentLocationStruct]? ){
self.yOffset = yOffset
self.components = components!
}
}
func buildComponentArray() {
componentArray.append( rowOfComponents( yOffset: 190, components: [
UIComponentLocationStruct( control: secondsLabel.frame, xOffset: 0, yOffset: 0 ),
UIComponentLocationStruct( control: tenthsLabel.frame, xOffset: 250, yOffset: 0 )] ) )
}
func updateControlLocations () {
let width = self.view.bounds.width
let xOffset: Int = Int( (width - 280) / 2 )
for i in 0 ..< componentArray.count {
for ii in 0 ..< componentArray[i].components.count {
var frame:CGRect = componentArray[i].components[ii].control
frame.origin.x = CGFloat( xOffset + componentArray[i].components[ii].xOffset )
frame.origin.y = CGFloat( componentArray[i].yOffset + componentArray[i].components[ii].yOffset )
componentArray[i].components[ii].control = frame // <-- This is not updating the UI element
}
}
}
我最终单独浏览了每个项目,这有效,但可能有更好的方法。