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.
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)
}
}
Aucun commentaire:
Enregistrer un commentaire