PanGesture错误的功能

时间:2017-07-18 23:35:38

标签: ios swift cocoa-touch

在最后一行代码上出错。

  

"无法指定类型' 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//
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

<强>更新 您的smallDot.image可能无法切换功能。

Then it should work like this

<强> 来源 该错误表示“无法指定类型'CGPoint'的值来键入'UIImageView!'”CGPoint的返回值为spawnRandomPosition()UIImageViewsmallDot

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