How to change the size of a uiimageview appended to a empty array

My swift code uses a button to place imageviews on a uiviewcontroller that parent from a empty array. The problem is I dont know how to effect the size of the invidicual imageview after it is placed on the uiview controller. When a imageview is click and then when the slider is moved the imageview should change size. However what is happening is the value when the slider is changed effects the next iamgeview that is placed on the uiviewcontroller. This is a video of what I am trying to do showcase - YouTube.

                      import UIKit

            class ViewController: UIViewController {

             var sx = UISlider()
           var count: Int = 0
         var ht = -90
          var ww = 80
           var moveCounter = 0
        var counter = 0
          var arrTextFields = [UIImageView]()
       var b7 = UIButton()


          override func viewDidLoad() {
super.viewDidLoad()

[b7,sx].forEach {
    $0.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview($0)
    $0.backgroundColor = .systemOrange
}

b7.frame = CGRect(x: view.center.x-115, y: view.center.y + 200, width: 70, height: 40)
sx.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: 70, height: 40)
b7.addTarget(self, action: #selector(addBOx), for: .touchUpInside)
           }

          //func that adds imageview.
         @objc func addBOx() {


      let subview = UIImageView()


subview.isUserInteractionEnabled = true
arrTextFields.append(subview)
view.addSubview(subview)



  sx.addTarget(self, action: #selector(ji), for: .valueChanged)
  sx.minimumValue = 10
  sx.maximumValue = 150

  subview.frame = CGRect(x: view.bounds.midX - 0, y: view.bounds.midY + CGFloat(ht), width: CGFloat(ww), height: 35)
  subview.backgroundColor = .purple
subview.tag = count
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:)))


subview.addGestureRecognizer(pan)



count += 1
ht += 50
arrTextFields.append(subview)





                    }

            @objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {


let draggedView = gesture.view!
view.bringSubviewToFront(draggedView)


let translation = gesture.translation(in: view)
draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
gesture.setTranslation(.zero, in: view)
                 }

           @objc func ji(sender : UISlider){
         ww = Int(sx.value)

            }}

Hi @timswift,
An element added to an array is not much different than an element stored in a variable directly.

So,

let a = UIImageView()
let b = UIImageView()

is the same as

let arr = [UIImageView(), UIImageView()] 
let a = arr[0]
let b = arr[1]

So you can get the reference to the element in the array and when you change the value of the slider, you can directly affect the frame/bounds/properties of that variable.

Hope that helps,

I am confused on what function to change?

hi @timswift,
you will need to change the currentView with the onTapGesture when one of the purple one is selected. Next you need to write the code in ji to change the width as per the slider’s value.

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.