斯威夫特:防止tic tac脚趾双击?

时间:2014-08-12 23:46:38

标签: swift uigesturerecognizer

我在Swift有一个tic tac toe游戏,我需要一种方法来禁用已经播放的空间上的轻击手势识别,以便在用户轮流时,用户不能只是点击已播放的地方。

我试过把这行

ticTacImages[spot].removeGestureRecognizer(UITapGestureRecognizer(target: self, action: "imageClicked:"))

在我的imageClickedsetImageForSpot函数中,没有任何反应。我在这里做错了什么?

涉及的代码:

for imageView in ticTacImages {

            imageView.userInteractionEnabled = true
            imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "imageClicked:"))

        }
    }

    //Gesture Reocgnizer method
    func imageClicked(reco: UITapGestureRecognizer) {

        var imageViewTapped = reco.view as UIImageView

        println(plays[imageViewTapped.tag])
        println(aiDeciding)
        println(done)

        opening1.hidden = true
        opening2.hidden = true
        opening3.hidden = true

        if plays[imageViewTapped.tag] == nil && !aiDeciding && !done {
            setImageForSpot(imageViewTapped.tag, player:.UserPlayer)
            }

        checkForWin()

        let delay = 1 * Double(NSEC_PER_SEC)
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        //During delay
        for imageView in ticTacImages {
            imageView.userInteractionEnabled = false
        }
        dispatch_after(time, dispatch_get_main_queue(), {
        //After delay
        for imageView in self.ticTacImages {
            imageView.userInteractionEnabled = true
            }
        self.aiTurn()
        })

    }
 var varChanger: Int?
    var playerMark: String?

    func setImageForSpot(spot:Int,player:Player){
        if varChanger == 1 {
            playerMark = player == .UserPlayer ? "blue_x" : "blue_o"
        }
        else if varChanger == 2 {
            playerMark = player == .UserPlayer ? "green_x" : "green_o"
        }
        else if varChanger == 3 {
            playerMark = player == .UserPlayer ? "purple_x" : "purple_o"
        }
        else if varChanger == 4 {
            playerMark = player == .UserPlayer ? "pink_x" : "pink_o"
        }
        else if varChanger == 5 {
            playerMark = player == .UserPlayer ? "yellow_x" : "yellow_o"
        }
        else {
            playerMark = player == .UserPlayer ? "red_x" : "red_o"
        }


        println("setting spot \(player.toRaw()) spot \(spot)")
        plays[spot] = player.toRaw()

        ticTacImages[spot].image = UIImage(named: playerMark)

    }

1 个答案:

答案 0 :(得分:2)

对于已添加的手势识别器,您只能removeGestureRecognizer()。在您的示例中,您在删除它之前创建了一个新的 - 而是应该跟踪旧的,或者调用ticTacImages[spot].gestureRecognizers以获取已添加的数组。