Home > Back-end >  how to safe UIView after tapped with UITapGestureRecognizer
how to safe UIView after tapped with UITapGestureRecognizer

Time:11-28

I try to add UIView for every tap on screen, but once I tapped UIView is added, but I can't safe him on screen , he is gone when I untapped. What should I do for safe UIView on screen and tap once to add second UIView and every my UIView saved?

import UIKit

class ViewController: UIViewController {
    
    // MARK: - Property
    
    let circle: UIView = {
        let circle = UIView()
        circle.frame.size.height = 100
        circle.frame.size.width = 100
        circle.layer.borderWidth = 10
        circle.layer.borderColor = UIColor.white.cgColor
        circle.layer.cornerRadius = 50
        return circle
    }()
    
    // MARK: - LifeCycle

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addRecognizer()
        view.backgroundColor = .red
    }
    
    // MARK: - Methods
    
    @IBAction func tappedRecognizer(_ recognizer: UITapGestureRecognizer) {
        
        let tapLocation = recognizer.location(in: view)
        let circleHeight = circle.frame.size.height
        let circleWidth = circle.frame.size.width
        print(tapLocation)
        circle.frame.origin = .init(x: tapLocation.x - circleWidth/2, y: tapLocation.y - circleHeight/2)
        view.addSubview(circle)
    }
    
    func addRecognizer() {
        
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(tappedRecognizer(_:)))
        self.view.addGestureRecognizer(recognizer)
    }
    
}

CodePudding user response:

Add array in your class and method to create new circle :

class ViewController: UIViewController {

    // MARK: - Property
    var circles = [UIView]()

    func newCircle(atLocation tapLocation: CGPoint) -> UIView {
        let circle = UIView()
        circle.frame.size.height = 100
        circle.frame.size.width = 100
        circle.layer.borderWidth = 10
        circle.layer.borderColor = UIColor.white.cgColor
        circle.layer.cornerRadius = 50
        circle.frame.origin = .init(x: tapLocation.x - circle.frame.size.width/2, 
                                    y: tapLocation.y - circle.frame.size.height/2)
        circles.append(circle)
        return circle
    }

    @IBAction func tappedRecognizer(_ recognizer: UITapGestureRecognizer) {
        let tapLocation = recognizer.location(in: view)
        print(tapLocation)
        let circle = newCircle(atLocation: tapLocation)
        view.addSubview(circle)
        }
    }

An other ways is to use a computed property (this may be what you tried to achieve). To do so, just remove the = from property definition :

    let circle: UIView { /: no =
        get {
            let circle = UIView()
            circle.frame.size.height = 100
            circle.frame.size.width = 100
            circle.layer.borderWidth = 10
            circle.layer.borderColor = UIColor.white.cgColor
            circle.layer.cornerRadius = 50
            return circle
        }
    } // no ()
  • Related