0
我正在循環[UIView],設置它們的框架,然後將它們作爲子視圖添加到UIScrollView。在代碼中,我指定一個隨機的背景顏色,所以我可以區分彼此的意見,用於測試目的:在for循環中創建CGRect並將其分配給UIView
for i in 0...questionViews.count - 1 {
let hue: CGFloat = CGFloat(arc4random() % 256)/256
let saturation: CGFloat = CGFloat(arc4random() % 128)/256 + 0.5
let brightness: CGFloat = CGFloat(arc4random() % 128)/256 + 0.5
questionViews[i].backgroundColor = UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
questionViews[i].frame = CGRect(x: screen.width * CGFloat(i), y: 0, width: screen.width, height: screen.height)
questionsScrollView!.addSubview(questionViews[i])
}
但是,如果我遍歷這些,並打印出來:
for i in 0...questionViews.count - 1 {
print(questionViews[i].frame)
}
結果將是:
(3000.0, 0.0, 375.0, 667.0)
(3000.0, 0.0, 375.0, 667.0)
(3000.0, 0.0, 375.0, 667.0)
(3000.0, 0.0, 375.0, 667.0)
(3000.0, 0.0, 375.0, 667.0)
(3000.0, 0.0, 375.0, 667.0)
(3000.0, 0.0, 375.0, 667.0)
(3000.0, 0.0, 375.0, 667.0)
(3000.0, 0.0, 375.0, 667.0)
爲什麼每個CGRect在for循環中都有最終的x值?
編輯:
的questionViews
陣列被設置在init與剛剛空CGRects
開始與:
questionViews = [UIView](count: numberOfQuestions, repeatedValue: UIView(frame: CGRect()))
請註明您是如何創建'questionViews'陣列 –
@SamM:更新:) – KamaKAzii