简体   繁体   English

比较字典中的元素,Swift

[英]Compare element in Dictionary, Swift

During the generation of my elements I also generate a Dictionary with [key,value]在生成元素期间,我还使用 [key,value] 生成字典

key is an Int键是一个 Int

Value is a string.值是一个字符串。

I gave an example: [1: A, 2: B, 3: A, 4: B]我举了一个例子:[1:A,2:B,3:A,4:B]

So now I know that first and third elements are a pairs!所以现在我知道第一个和第三个元素是一对!

I also have a function that rotates my element when tapped.我还有一个 function 可以在点击时旋转我的元素。 When I tap a card I have access to his ID.当我点击一张卡片时,我可以访问他的 ID。

What I need is to compare if the tapped card has the same dictionary value of the second tapped card (and if yes don't rotate anymore this card) else rotate down both card.我需要比较的是被敲击的卡片是否与第二张被敲击的卡片具有相同的字典值(如果是,则不再旋转这张卡片),否则向下旋转两张卡片。

This is the function I am talking about:这就是我说的function:

    @IBAction func actionPerformed(_ sender: UITapGestureRecognizer) {
        if element.transform.rotation.angle == .pi {
            self.firstRotation(element: element)

                } else {
                    self.secondRotation(element: element)

                }

}

So lets wrap up, I already have a dictionary with solutions, what I need is to check if the first tapped card is equal to the second, if yes do something, else do other thing.所以让我们总结一下,我已经有一本包含解决方案的字典,我需要检查第一张被点击的卡片是否等于第二张卡片,如果是,则做某事,否则做其他事情。

A possible solution is to save the ID of the card to a variable ( firstId ).一种可能的解决方案是将卡的 ID 保存到变量 ( firstId )。 If this variable is nil , it means that this is the first tapped card, else it is the second one.如果此变量为nil ,则表示这是第一张横置的牌,否则是第二张。

var firstId: Int?

@IBAction func onTap(_ sender: UITapGestureRecognizer) {
    let tapLocation = sender.location(in: arView)
    if let card = arView.entity(at: tapLocation) {
        if let firstId = firstId {
            // second card tapped
            if self.cardEntityCorrelation[firstId] == self.cardEntityCorrelation[card.id] {
                // cards form a pair; no rotation is needed
                // reset firstId to allow selecting the next pair
                self.firstId = nil
            } else {
                // cards do not form a pair
                // rotate second card
                rotate(card: card)
            }
        } else {
            // first card tapped; save id
            firstId = card.id
            // rotate first card
            rotate(card: card)
        }
    }
}

//ROTATE UP OR DOWN
private func rotate(card: Entity) {
    if card.transform.rotation.angle == .pi {
        firstRotation(card: card)
    } else {
        secondRotation(card: card)
    }
}

//ROTATE UP
private func firstRotation(card: Entity) {
    var flipDownTransform = card.transform
    flipDownTransform.rotation = simd_quatf(angle: 0, axis: [1,0,0])
    card.move(to: flipDownTransform, relativeTo: card.parent, duration: 0.25, timingFunction: .easeInOut)
}

//ROTATE DOWN
private func secondRotation(card: Entity) {
    var flipUpTransform = card.transform
    flipUpTransform.rotation = simd_quatf(angle: .pi, axis: [1,0,0])
    card.move(to: flipUpTransform, relativeTo: card.parent, duration: 0.25, timingFunction: .easeInOut)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM