【发布时间】:2016-01-15 01:36:13
【问题描述】:
每次单击屏幕时,我都会尝试在 Swift 中的随机 CGPoint 上显示一个正方形。下面是我在没有编译器错误的情况下所做的,但是一旦我运行应用程序,我可以单击或拖动来移动我的“人”,但每次我这样做时,都不会出现一个较小的正方形。任何建议或解决方案都很棒。谢谢!顺便说一句,人,UIImageView 不是图像,但有一个可行的背景颜色。`
`
@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
}
}`
【问题讨论】: