在代码的最后一行出现错误。

“无法将类型'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//
        }
    }
}

ios - 手势错误功能-LMLPHP

最佳答案

更新
您的smallDot.image可能为零,无法切换功能。

ios - 手势错误功能-LMLPHP

来源
该错误表明“无法将类型'CGPoint'的值分配给类型'UIImageView!'”,其中CGPointspawnRandomPosition()的返回值,而UIImageViewsmallDot

试试这个:smallDot.center = spawnRandomPosition()

10-08 06:09