简体   繁体   中英

UIView is Being Over Written Every Time Func is Called

My swift codes goal is to place a uiview every time the button is pressed. In my gif you can see every time the blue button is called it is over written. When the code is pressed the gif should have 2 uiviews in it. You can see the transparent uiview of where the first view disappears. Basically all that is wrong with this code is when the addBlackView is called it should add to the views on the screen basically just like a infinite array.

在此处输入图片说明

import UIKit
class ViewController: UIViewController {

var image1Width2: NSLayoutConstraint!
var iHieght: NSLayoutConstraint!

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white

    view.addSubview(slider)
    slider.translatesAutoresizingMaskIntoConstraints = false
    slider.value = 0.5
    slider.isUserInteractionEnabled = true
    NSLayoutConstraint.activate([
        slider.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        slider.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        slider.heightAnchor.constraint(equalToConstant: 100),
        slider.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1),
    ])

    view.addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
        button.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16),
        button.widthAnchor.constraint(equalToConstant: 100),
        button.heightAnchor.constraint(equalToConstant: 80),
    ])
    button.addTarget(self,action: #selector(addBlackView),for: .touchUpInside)
    slider.addTarget(self, action: #selector(increase), for: .allEvents)

}

let slider:UISlider = {
    let slider = UISlider(frame: .zero)
    return slider
}()

private lazy var button: UIButton = {
    let button = UIButton()
    button.backgroundColor = .blue
    button.setTitleColor(.white, for: .normal)
    button.setTitle("add", for: .normal)
    return button
}()

let blackView: UIView = {
    let view = UIView()
    view.backgroundColor = .black
    return view
}()

@objc
private func addBlackView() {
    self.view.addSubview(blackView)
    blackView.translatesAutoresizingMaskIntoConstraints = false
    blackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    blackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    image1Width2 = blackView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.1)
    image1Width2.isActive = true
    iHieght = blackView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1)
    iHieght.isActive = true
    view.layoutIfNeeded()

    let recognizer = UIPanGestureRecognizer(target: self, action: #selector(moveView(_:)))
    blackView.addGestureRecognizer(recognizer)
}

@objc private func moveView(_ recognizer: UIPanGestureRecognizer) {
    switch recognizer.state {
    case .began:
        print("gesture began")
    case .changed:
        let translation = recognizer.translation(in: self.view)

        recognizer.view!.center = .init(x: recognizer.view!.center.x + translation.x,
                                        y: recognizer.view!.center.y + translation.y)
        recognizer.setTranslation(.zero, in: self.view)
    default:
        break
    }
}

@objc func increase() {
    image1Width2.constant = CGFloat(slider.value) * view.frame.size.width * 0.10
    iHieght.constant = CGFloat(slider.value) * view.frame.size.width * 0.10
}}

The problem is that you're reusing and resetting blackView every time you execute addBlackView , so the changes you've made will be lost (hence why the view goes back in the center after you pressed the button).

You would need to create a complete new view in addBlackView , which would be your 'currentView' that you are manipulating and then add add gesture recognizers to it. Then once you execute addBlackView again, the 'currentView' would be 'validated' (stored in an array or whatever you need to do with it) and then you create another one to manipulate.

Something like this:

private func addBlackView() {
    let newBlackView = UIView(frame: CGRect(0, 0, 10, 10)) // whatever frame you want
    self.view.addSubview(newBlackView)
    self.currentView = newBlackView
}

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