【发布时间】:2020-03-20 01:48:22
【问题描述】:
我想在每次调用 func moveRight 时使用我的快速代码来放置一个图像视图。我希望每个新的 imageView 在 y 轴上用 50 分隔。现在我的代码可以编译,但调用函数时没有任何变化。但在调试区域,我看到计数在增加。
import UIKit
class ViewController: UIViewController {
var myArray = [UIImageView]()
var bt = UIButton()
var count : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(bt)
bt.backgroundColor = UIColor.systemOrange
bt.frame = CGRect(x: view.center.x - 0, y: view.center.y , width: 50, height: 50)
bt.addTarget(self, action: #selector(moveRight), for: .touchUpInside)
}
@objc func moveRight() {
print("Yes")
myArray.forEach({
$0.backgroundColor = UIColor.systemTeal
self.view.addSubview($0)
})
var ht = 50
myArray.insert(UIImageView(), at: count)
print("Your Count is ", count)
myArray[count].frame = CGRect(x: view.center.x - 0, y: view.center.y + CGFloat(ht), width: 50, height: 50)
count += 1
ht += 50
}
}
【问题讨论】:
标签: arrays swift for-loop insert int