gpt4 book ai didi

swift - 在 Swift 中以编程方式将图像添加/删除到 subview

转载 作者:行者123 更新时间:2023-11-28 05:47:56 34 4
gpt4 key购买 nike

使用此处另一篇文章中的代码,我能够以编程方式绘制和删除 subview ,包括位置、宽度和高度,如下面的 func addLoadButton 和 func removeSubview 所示。我还想出了如何以编程方式在主视图 Controller 上绘制图片,如下面的 func trailerLoadImage 所示。然而,经过数小时的尝试,我尝试以编程方式向该 subview 中添加和删除图像,但没有成功。

我的最终目标是能够按下三个不同的拖车加载类型按钮以在位于屏幕上特定位置的 subview 中插入三个不同的图像(按钮 1 加载图像 1,按钮 2 加载图像 2,等等) ,并能够通过用手指点击图像一次删除一个图像(可能不是按顺序放在屏幕上)。 subview 可以是永久性的,也可以通过编程方式创建和删除(如下所用)。

我将使用什么代码将一个或多个不同的图像插入到已创建的 subview 中,以相反的顺序删除图像,并清除 subview 中的所有图像?如果无法做到这一点,可接受的替代方法是通过点击图像或按下按钮来清除所有添加的图像,从而从主 VC 中删除图像。

//Class declaration
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

//Boolean to include load type one in calculations
var trailerLoad : Bool = false
var trailerLoadDistanceFromFront = 20

//Boolean to include load type two in calculations
var trailerLoadTwo : Bool = false
var trailerLoadTwoDistanceFromFront = 80

//Boolean to include load type three in calculations
var trailerLoadThree : Bool = false
var trailerLoadThreeDistanceFromFront = 120

var trailerLoadWidth : Int = 0
var trailerLoadX : Int = 0

//Boolean true only when subView on trailer is active
var subViewActive : Bool = false


override func viewDidLoad() {
super.viewDidLoad()

//Picker view data sources and delegates included in here and work fine
}


//Adds subview for loads
@IBAction func addLoadButton(_ sender: Any) {

let trailerLoadView: UIView = UIView(frame: CGRect(x: 252, y: 233, width: 378, height: 100))
trailerLoadView.backgroundColor = .blue
trailerLoadView.alpha = 0.5
trailerLoadView.tag = 100
trailerLoadView.isUserInteractionEnabled = true
self.view.addSubview(trailerLoadView)

subViewActive = true
}


//If subViewActive is true, calls alert to get distance load type one is from front, moves on to insert and position image, changes trailerLoad bool to true
@IBAction func trailerLoadOneButton(_ sender: Any) {
//If subViewActive is true:
//Calls alert to get distance load type one is from front, puts in var trailerLoadDistanceFromFront
//Calls trailerLoadImage() to insert and position load type one image
//Changes bool trailerLoad to true
//If subViewActive is false:
//Calls alert to tell user that they need to click Add Load button (create subview) before adding load types one, two, or three
}


//Add trailer load type one image, scales and positions it relatively accurately in view.
//To be duplicated and modified for load types two and three in the future, with different images (trailerLoadTypeTwoPic and trailerLoadTypeThreePic)
func trailerLoadImage() {

trailerLoadWidth = 378 * 60 / trailerTotalLength
trailerLoadX = 378 * trailerLoadDistanceFromFront / trailerTotalLength

let imageView = UIImageView(frame: CGRect(x: (252 + trailerLoadX), y: (333 - trailerLoadWidth), width: trailerLoadWidth, height: trailerLoadWidth));
let image = UIImage(named: “trailerLoadTypeOnePic”);

imageView.image = image;
self.view.addSubview(imageView)
}


//Calls func removeSubview to remove subview
@IBAction func resetButton(_ sender: Any) {

removeSubview()
}


//Removes subview for loads
@objc func removeSubview(){

subViewActive = false

if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}else{
print("No!")
}
}
}

非常感谢任何提供帮助或建议的人。

最佳答案

不要使用标签!只需在全局范围内为您的 View 创建变量

var imageViews = [UIImageView]()

然后当您需要添加它们时,首先将它们添加到您的数组中,然后将它们添加到view

imageViews.append(imageView)
view.addSubview(imageView)

然后,当您需要从其父 View 中删除所有 View 时,对数组中的每个 View 使用方法 removeFromSuperview()

imageViews.forEach { $0.removeFromSuperview() }
imageViews.removeAll()

或者如果您只需要删除特定索引处的一个 View

imageViews[index].removeFromSuperview()
imageViews.remove(at: index)

关于swift - 在 Swift 中以编程方式将图像添加/删除到 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53972437/

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