swift - 在 Swift 中以编程方式将图像添加/删除到子视图
问题描述
使用此处另一篇文章中的代码,我能够以编程方式绘制和擦除子视图,包括位置、宽度和高度,如下面的 func addLoadButton 和 func removeSubview 所示。我还想出了如何以编程方式在主视图控制器上绘制图片,如下面的 func trailLoadImage 所示。但是,经过数小时的尝试和尝试,我尝试以编程方式将图像添加和删除到该子视图中,但没有成功。
我的最终目标是能够按三个不同的预告片加载类型按钮在位于屏幕特定位置的子视图中插入三个不同的图像(按钮 1 加载图像 1,按钮 2 加载图像 2,等等),并可以通过用手指点击图像一次删除一个图像(可能不是按顺序放在屏幕上)。子视图可以是永久性的,也可以通过编程方式创建和删除(如下所示)。
我将使用什么代码将图像或多个不同的图像插入到已经创建的子视图中,以添加的相反顺序删除图像,并清除子视图中的所有图像?如果无法做到这一点,可接受的替代方法是通过点击主 VC 或按下按钮清除所有添加的图像来从主 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!")
}
}
}
非常感谢任何提供帮助或建议的人。
解决方案
不要使用标签!只需在全局范围内为您的视图创建变量
var imageViews = [UIImageView]()
然后当您需要添加它们时,首先将它们附加到您的数组中,然后将它们添加到view
imageViews.append(imageView)
view.addSubview(imageView)
然后,当您需要从其父视图中删除所有视图时,请removeFromSuperview()
对数组中的每个视图使用方法
imageViews.forEach { $0.removeFromSuperview() }
imageViews.removeAll()
或者如果您只需要删除特定索引处的一个视图
imageViews[index].removeFromSuperview()
imageViews.remove(at: index)
推荐阅读
- swift - URLSession 异步在 Swift 包中不起作用
- web - 我们的前端怎么知道这个http响应属于我们的请求呢?
- java - 在 Android 12 (SDK 31) 中获取 android.app.ForegroundServiceStartNotAllowedException
- oauth-2.0 - 如何在 Postman 中设置多个 Oauth2 范围
- authentication - 如何不在具有相同身份验证服务器的不同 webapps 之间共享身份验证 cookie
- django - 在 django 中验证 url 中的日期的最佳实践?
- python - 如何返回给定 N*N 方阵的对角线或对角线数组
- function - 使用海关间隔研究功能误差
- html - 如何更改输入按钮(方形)的默认外观,使其看起来像三个垂直点?
- php - 我总是得到一个响应'mysqli_stmt object is not fully initialized'作为下面代码的输出