Search code examples
arraysswiftif-statementuiviewuipangesturerecognizer

Add items to array Swift


My swift code right now uses func addbox to append imageviews to a empty array of imageviews.

When the user taps button widthActivatorBtn it should allow the user to select on a imageview and change the width of the imageveiew. It works but only if the widthActivatorBtn was selected and selectNum is changed to 3.

I can affect imageview placed on after selectNum was changed to 3 but not the imageviews before it. Check out my gif and you can see the problem. When selectNum is changed to 3 all imageviews should be able to have their width changed via slider.

enter image description here

import UIKit

class ViewController: UIViewController {

    var slider = UISlider()
    var ht = -90
    var widthSize = 80
    var emptyArray = [UIImageView]()
    var addImageview = UIButton()
    var width = UIButton()
    var currentView: UIView?
    var selectNum = Int()

    override func viewDidLoad() {
        super.viewDidLoad()
        [addImageview,slider,width].forEach {
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            $0.backgroundColor = .systemOrange
        }

        addImageview.setTitle("add imageview", for: .normal)

        width.setTitle("change width", for: .normal)
        addImageview.frame = CGRect(x: view.center.x-115, y: view.center.y + 200, width: 160, height: 40)
        slider.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: 160, height: 40)

        width.frame = CGRect(x: view.center.x-115, y: view.center.y + 100, width: 160, height: 40)
        addImageview.addTarget(self, action: #selector(addBOx), for: .touchUpInside)
        width.addTarget(self, action: #selector(widthActivatorBtn), for: .touchUpInside)
        slider.addTarget(self, action: #selector(ji), for: .valueChanged)
        slider.minimumValue = 10
        slider.maximumValue = 150

    }

    @objc func widthActivatorBtn() {

        width.backgroundColor = .systemPink

        selectNum = 3
    }

    @objc func addBOx() {


        let subview = UIImageView()


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

        subview.frame = CGRect(x: view.bounds.midX - 0, y: view.bounds.midY + CGFloat(ht), width: CGFloat(widthSize), height: 35)
        subview.backgroundColor = .purple


        ht += 50
        emptyArray.append(subview)

        if  selectNum == 3{
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:)))
            subview.addGestureRecognizer(tapGesture)

        }


    }

    @objc func handleTapGestured(_ gesture: UIPanGestureRecognizer) {
        currentView = gesture.view

    }


    @objc func ji(sender : UISlider){
        widthSize = Int(slider.value)
        currentView?.bounds.size.width = CGFloat(slider.value)

    }

}

Solution

  • remove selectNum variable, it is causing the problem. when you are creating the first image at that time selectNum is not equal to 3, so that the gesture is not added to the first imageView, but after cliking on width button selectNum set to value 3 and after that when you create any imageview then that imageView gets the gesture and you width logic works.

    @objc func addBOx() {
    
    
            let subview = UIImageView()
    
    
            subview.isUserInteractionEnabled = true
            emptyArray.append(subview)
            view.addSubview(subview)
    
            subview.frame = CGRect(x: view.bounds.midX - 0, y: view.bounds.midY + CGFloat(ht), width: CGFloat(widthSize), height: 35)
            subview.backgroundColor = .purple
    
    
            ht += 50
            emptyArray.append(subview)
    
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:)))
            subview.addGestureRecognizer(tapGesture)
    
        }
    
    
    @objc func widthActivatorBtn() {
    
            width.backgroundColor = .systemPink
    
            selectNum = 3
        }
    
        @objc func ji(sender : UISlider){
          if selectNum == 3 { 
            widthSize = Int(slider.value)
            currentView?.bounds.size.width = CGFloat(slider.value)
          }
        }
    

    add the condition selectNum == 3 in ji func, to resize. and you can reset selectNum somewhere to stop resizing. so now when widthActivatorBtn will be pressed then only width will change. hope this will work for you.