在最后一行代码上出错。
"无法指定类型' CGPoint'的值输入' UIImageView!'"
我尝试使用smallDot.center = spawnRandomPosition()
,但是当我运行它时,smallDot并没有在随机位置产生。
class SecondViewController: UIViewController {
private var addOne = 0
func spawnRandomPosition() -> CGPoint {
let height = self.view!.frame.height
let width = self.view!.frame.width
let randomPosition = CGPoint(x:CGFloat(arc4random()).truncatingRemainder(dividingBy: height),
y: CGFloat(arc4random()).truncatingRemainder(dividingBy: width))
return randomPosition
}
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
if (WhiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
smallDot.image = nil;
addOne += 1
score.text = "\(addOne)";
smallDot = spawnRandomPosition() //error here//
}
}
}