在Swift中以编程方式向子视图添加/删除图像

时间:2018-12-29 18:51:20

标签: swift image subview

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

我的最终目标是能够按三个不同的预告片加载类型按钮在屏幕上特定位置的子视图中插入三个不同的图像(按钮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!")
        }
    }
}

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

1 个答案:

答案 0 :(得分:1)

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

var imageViews = [UIImageView]()

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

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

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

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

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

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