在Swift中点击一下

时间:2022-03-14 01:09:06

I am trying to make a square appear on a random CGPoint in Swift every time I click the screen. Below is what I have done with no compiler errors, but once I run the application, I can click or drag to move my "person," but every time I do that, a smaller square does not appear. Any advice or solutions are great. Thank you! by the way, person, a UIImageView is no image but has a viable backgroundColor.`

我每次点击屏幕时都试图在Swift中的随机CGPoint上出现一个正方形。下面是我没有编译器错误所做的事情,但是一旦我运行应用程序,我可以单击或拖动来移动我的“人物”,但每次我这样做时,都不会出现较小的正方形。任何建议或解决方案都很棒。谢谢!顺便说一句,人,一个UIImageView是没有图像,但有一个可行的backgroundColor

`

    @IBOutlet weak var person: UIImageView!

    var location = CGPoint(x: 0,y: 0)

    var randomPoint = CGPoint(x:Int(arc4random()%1000),y:Int(arc4random()%1000))

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let touch = touches.first

        location = touch!.locationInView(self.view)

        person.center = location

        for _ in touches {

            let mySquare: UIView = UIView(frame: CGRect(x: 0,y: 0,width: 20,height: 20))

            mySquare.backgroundColor = UIColor.cyanColor()

            mySquare.center = randomPoint

        }

    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let touch = touches.first

        location = touch!.locationInView(self.view)

        person.center = location

        for _ in touches {

            let mySquare: UIView = UIView(frame: CGRect(x: 0,y: 0,width: 20,height: 20))

            mySquare.backgroundColor = UIColor.cyanColor()

            mySquare.center = randomPoint

        }
    }`

2 个解决方案

#1


0  

For every touch, you just need create and add(you missed this step) square in touch begin (or touch end), don't do in touch move.

对于每一次触摸,你只需要创建和添加(你错过了这一步)方形触摸开始(或触摸结束),不要触摸移动。

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let touch = touches.first

        location = touch!.locationInView(self.view)

        person.center = location

        for _ in touches {

            let mySquare: UIView = UIView(frame: CGRect(x: 0,y: 0,width: 20,height: 20))

            mySquare.backgroundColor = UIColor.cyanColor()

            mySquare.center = randomPoint

            view.addSubview(mySquare) // add square to the view

        }

    }

#2


0  

You didn't add mySquare view onto any view.

您没有将mySquare视图添加到任何视图中。

 self.view.addSubview(mySquare)

#1


0  

For every touch, you just need create and add(you missed this step) square in touch begin (or touch end), don't do in touch move.

对于每一次触摸,你只需要创建和添加(你错过了这一步)方形触摸开始(或触摸结束),不要触摸移动。

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let touch = touches.first

        location = touch!.locationInView(self.view)

        person.center = location

        for _ in touches {

            let mySquare: UIView = UIView(frame: CGRect(x: 0,y: 0,width: 20,height: 20))

            mySquare.backgroundColor = UIColor.cyanColor()

            mySquare.center = randomPoint

            view.addSubview(mySquare) // add square to the view

        }

    }

#2


0  

You didn't add mySquare view onto any view.

您没有将mySquare视图添加到任何视图中。

 self.view.addSubview(mySquare)