简体   繁体   中英

Create a copy of UIImageView swift

I want to be able to call a function and have that function create a copy of a UIImageView . This is where I construct the image I want to be copied.

    var redBar = UIImageView()
    let redBarImage = UIImage(named: "Music Bar")
    redBar = UIImageView(image: redBarImage)
    self.addSubview(redBar)

When the user does a pan gesture (this part I have done), I want to call a function called "produceRedBar" like this:

func produceRedBar(){
}

The redBar is a global variable. How can I produce a copy of the redBar , but it needs to produce a new copy every time the function is called.

Instead of copying image views etc why don't you try this,

func produceRedBar(value: Int){

    for i in 0...value{
        let newView = UIImageView(image: UIImage(named: "Music Bar"))
        var rectImage:CGRect = newView.frame
        rectImage.origin.x = 10
        rectImage.origin.y = CGFloat(10 + (i * 20) + 5) //10 - origin + i * 20 - for placing + 5 - padding
        self.view.addSubview(newView)
    }

}

and call your produceRedBar() method with the count of the volume value like this,

self.produceRedBar(5)
self.produceRedBar(4)
self.produceRedBar(6)

Note: adjust x and y values accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM