UIPanGestureRecognizer即时反馈

时间:2017-02-16 20:24:56

标签: ios swift swift3

所以这段代码正在移动我的块(UIImages)很好,但是,当我移动某个彩色块(我们说明亮的绿色块)超过匹配的亮绿色圆圈时,没有任何反应直到我开始移动另一个块,然后它最终识别出前一个移动是一个匹配。我是否在功能的错误位置进行了匹配检测(以下)?

if (100 / 2 > sqrt((pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y))) && 
    myBlocks[objectDragging!].tintColor == myCircles[myBlocks[objectDragging!].tag].tintColor 

...

@IBAction func handlePan(_ recognizer: UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: self.view)
    let objectDragging = recognizer.view?.tag
    let pos1 = myBlocks[objectDragging!].center
    let pos2 = myCircles[myBlocks[objectDragging!].tag].center

    if (recognizer.state == UIGestureRecognizerState.began || recognizer.state == UIGestureRecognizerState.changed) && ((100 / 2 > sqrt((pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y))) && myBlocks[objectDragging!].tintColor == myCircles[myBlocks[objectDragging!].tag].tintColor) {

        print("match")
        myBlocks[objectDragging!].isHidden = true
        myCircles[myBlocks[objectDragging!].tag].tintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

        print(objectDragging ?? 20)
        //print(myBlocks[objectDragging!])

        print("the tag of the object your touching is \(myBlocks[objectDragging!])")
    }


    else if recognizer.state == UIGestureRecognizerState.ended {

        if (100 / 2 > sqrt((pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y))) && myBlocks[objectDragging!].tintColor == myCircles[myBlocks[objectDragging!].tag].tintColor {
            print("match")
            myBlocks[objectDragging!].isHidden = true
            myCircles[myBlocks[objectDragging!].tag].tintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        }


    }
    else {

        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 object matches object in recognizer
    }
}

编辑&行为GIF 1GIF 2: 请注意第一个我如何进行匹配,一旦移动下一个块,它就会记录该匹配。但是,在第二个gif中你会注意到,如果你在比赛后移动的棋子显示了它的圆圈,那么它似乎只能注册比赛。我的意思是我做了比赛,然后移了几个街区,什么都没做,直到我移动那个暗红色的那个(通过移动绿色块来揭示谁的圆圈)。所以它似乎只在匹配之后匹配,并且另一个块具有它显示的相应圆圈被移动。很奇怪。

@IBAction func handlePan(_ recognizer: UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: self.view)
    let objectDragging = recognizer.view?.tag

    let pos1 = myBlocks[objectDragging!].center
    let pos2 = myCircles[myBlocks[objectDragging!].tag].center


    switch(recognizer.state) {
    case .began:
        if ((100 / 2 > sqrt((pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y))) && myBlocks[objectDragging!].tintColor == myCircles[myBlocks[objectDragging!].tag].tintColor) {

            myBlocks[objectDragging!].isHidden = true
            myCircles[myBlocks[objectDragging!].tag].tintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
            print(".began")

        }
    case .changed:

        if ((100 / 2 > sqrt((pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y))) && myBlocks[objectDragging!].tintColor == myCircles[myBlocks[objectDragging!].tag].tintColor) {

            myBlocks[objectDragging!].tintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.0)
            myCircles[myBlocks[objectDragging!].tag].tintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
            print(".changed")

        }

    case .ended:

        if ((100 / 2 > sqrt((pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y))) && myBlocks[objectDragging!].tintColor == myCircles[myBlocks[objectDragging!].tag].tintColor) {

            myBlocks[objectDragging!].isHidden = true
            myCircles[myBlocks[objectDragging!].tag].tintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
            print(".ended")

        }

    default:
        break
    }

    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)
}

0 个答案:

没有答案