Label et Button Invisibles

HooliVR

Membre confirmé
24 Mars 2020
27
1
27
Bonjour à tous,

Je poste un message ici car j'ai un problème, impossible de voir les Label ou tout autre objet de la Library,
j'ai l'impression que ma vue passe par dessus..

Quelqu'un pourrait m'éclairer stp ?

Je vous met le code ci dessous :

Bloc de code:
import UIKit

class TESTtouchViewController: UIViewController {

    
    
    override func loadView() {
            view = TouchableView()
        view.backgroundColor = .black
        }
    }

    class TouchableView: UIView {
        var touchViews = [UITouch:TouchSpotView]()

        
        override init(frame: CGRect) {
            super.init(frame: frame)
            isMultipleTouchEnabled = true
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            isMultipleTouchEnabled = true
        }

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            for touch in touches {
                createViewForTouch(touch: touch)
                print(String(touchViews.count + 1) + " doigt(s) sur l'écran")
            }
        }

        override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
            for touch in touches {
                let view = viewForTouch(touch: touch)
                // Move the view to the new location.
                let newLocation = touch.location(in: self)
                view?.center = newLocation
            }
        }

        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            for touch in touches {
                removeViewForTouch(touch: touch)
                print(String(touchViews.count - 1) + " doigt(s) sur l'écran")
            }
        }

        override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
            for touch in touches {
                removeViewForTouch(touch: touch)
                
                
            }
        }

        func createViewForTouch( touch : UITouch ) {
            let newView = TouchSpotView()
            newView.bounds = CGRect(x: 0, y: 0, width: 1, height: 1)
            newView.center = touch.location(in: self)

            // Add the view and animate it to a new size.
            addSubview(newView)
            UIView.animate(withDuration: 0.2) {
                newView.bounds.size = CGSize(width: 100, height: 100)
            }

            // Save the views internally
            touchViews[touch] = newView
        }

        func viewForTouch (touch : UITouch) -> TouchSpotView? {
            return touchViews[touch]
        }

        func removeViewForTouch (touch : UITouch ) {
            if let view = touchViews[touch] {
                view.removeFromSuperview()
                touchViews.removeValue(forKey: touch)
            }
        }
    }

    class TouchSpotView : UIView {
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = UIColor.lightGray
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        // Update the corner radius when the bounds change.
        override var bounds: CGRect {
            get { return super.bounds }
            set(newBounds) {
                super.bounds = newBounds
                layer.cornerRadius = newBounds.size.width / 2.0
                
            }
            
            }
    
        }
 
Dans ce post, je te précise en début de méthode (func) pas dans la boucle for ... in ...
Je pense que tu t'emmêles un peu (beaucoup) sur les vues, elles doivent être gérées par des contrôleurs.

Je te conseille d'aller voir sur le site d'Openclassroom, ils ont d'excellents cours qui traitent du sujet ;)