gpt4 book ai didi

ios - 根据位置删除图像的ScrollView

转载 作者:行者123 更新时间:2023-11-28 08:16:14 26 4
gpt4 key购买 nike

我制作了一个 ScrollView ,并制作了一个图像数组(稍后我将添加一个选项供用户将图像发布到该数组中,而 friend 的图像将填充到该数组中)并希望用户能够看到一张照片并跟踪它。

我创建了一个用于将照片添加到数组中的 for 循环,但是当我创建一个 if 语句来取出数组中的一个项目时,我得到了一个错误,因为我将从数组中删除图像 [i] 并且我有点知道为什么会这样,但想修复它。所以基本上我需要取出数组中的图像。
请帮助我,我还在学习。

class ViewController: UIViewController {

var imageArray = [UIImage]()

@IBOutlet weak var mainScrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()
mainScrollView.frame = view.frame
imageArray = [#imageLiteral(resourceName: "dubai6"), #imageLiteral(resourceName: "dubai2"), #imageLiteral(resourceName: "dubai7"), #imageLiteral(resourceName: "dubai4"), #imageLiteral(resourceName: "dubai3"), #imageLiteral(resourceName: "dubai5"), #imageLiteral(resourceName: "DigitalDrawingPreview"), #imageLiteral(resourceName: "denarus"), #imageLiteral(resourceName: "dubai1")]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.image = imageArray[i]
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.view.frame.width, height: self.mainScrollView.frame.height)

mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)
}
for i in 0..<imageArray.count {
if (i >= 2) {
imageArray.remove(at: (i - 2))
}
}

// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

谢谢你们更好的程序员。

最佳答案

遍历数组时不能从数组中删除元素,因此必须使用临时数组

class ViewController: UIViewController {

var imageArray = [UIImage]()

@IBOutlet weak var mainScrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()
mainScrollView.frame = view.frame
imageArray = [#imageLiteral(resourceName: "dubai6"), #imageLiteral(resourceName: "dubai2"), #imageLiteral(resourceName: "dubai7"), #imageLiteral(resourceName: "dubai4"), #imageLiteral(resourceName: "dubai3"), #imageLiteral(resourceName: "dubai5"), #imageLiteral(resourceName: "DigitalDrawingPreview"), #imageLiteral(resourceName: "denarus"), #imageLiteral(resourceName: "dubai1")]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.image = imageArray[i]
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.view.frame.width, height: self.mainScrollView.frame.height)

mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)


}
let tempArr = imageArray
for i in 0..<tempArr.count {
if (i >= 2) { // Breakpoint here
imageArray.remove(at: (i - 2))
}

}

// Do any additional setup after loading the view, typically from a nib.
}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

使用 step over 遍历 for 循环的每一行直到它崩溃,然后通过将光标悬停在它上面或在控制台 po i 键入命令并输入当时打印 i 的值 enter image description here

关于ios - 根据位置删除图像的ScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42523709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com