使用在此帖子中找到的代码,我能够以编程方式绘制和擦除一个子视图,包括位置、宽度和高度,如下所示的函数addLoadButton和removeSubview。我还发现了如何在主视图控制器上以编程方式绘制图片,如下所示的函数trailerLoadImage。然而,在许多小时和尝试之后,我尝试以编程方式添加和删除该子视图中的图像,但没有成功。
我的最终目标是能够按下三个不同的拖车装载类型按钮,将三个不同的图像(按钮1加载图像1,按钮2加载图像2等)插入位于屏幕特定位置的子视图中,并能够逐个删除这些图像(可能不按照放置顺序),通过用手指点击图像或按下清除所有添加的图像的按钮来实现。子视图可以是永久的,也可以是以编程方式创建和删除的(如下所示)。
我应该使用什么代码将图像或多个不同的图像插入到已经创建的子视图中,以相反的顺序删除添加的图像,并清除子视图中的所有图像?如果无法完成此操作,则可接受的替代方案是通过点击图像或按下清除所有添加的图像的按钮从主VC中删除图像。
非常感谢任何提供帮助或建议的人。
我的最终目标是能够按下三个不同的拖车装载类型按钮,将三个不同的图像(按钮1加载图像1,按钮2加载图像2等)插入位于屏幕特定位置的子视图中,并能够逐个删除这些图像(可能不按照放置顺序),通过用手指点击图像或按下清除所有添加的图像的按钮来实现。子视图可以是永久的,也可以是以编程方式创建和删除的(如下所示)。
我应该使用什么代码将图像或多个不同的图像插入到已经创建的子视图中,以相反的顺序删除添加的图像,并清除子视图中的所有图像?如果无法完成此操作,则可接受的替代方案是通过点击图像或按下清除所有添加的图像的按钮从主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!")
}
}
}
非常感谢任何提供帮助或建议的人。
self.view
只包含这些图像视图,则可以通过请求let ims = self.view.subviews.filter{$0 is UIImageView}
来获取它们的数组。现在,您已经按添加顺序获得了图像视图,并且可以更改其中一个的图像,将其从其父视图中删除等操作。 - matt