【问题标题】:Compare element in Dictionary, Swift比较字典中的元素,Swift
【发布时间】:2020-05-16 20:40:50
【问题描述】:

在生成元素的过程中,我还使用 [key,value] 生成字典

key 是一个 Int

值是一个字符串。

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

所以现在我知道第一个和第三个元素是一对!

我还有一个功能可以在点击时旋转我的元素。当我点击一张卡片时,我可以访问他的 ID。

我需要比较的是被点击的卡片是否与第二张被点击的卡片具有相同的字典值(如果是,则不再旋转这张卡片)否则向下旋转两张卡片。

这就是我说的函数:

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

                } else {
                    self.secondRotation(element: element)

                }

}

所以让我们结束,我已经有一个带有解决方案的字典,我需要检查第一张被点击的卡片是否等于第二张,如果是,则做某事,否则做其他事情。

【问题讨论】:

  • 尝试将第一个被点击的卡片的ID保存到一个变量中,例如firstId,然后比较dictionary[firstId]和dictionary[card.id]
  • 我有很多想法,你能给我一些代码吗?感谢您的回答顺便说一句

标签: ios arrays swift xcode dictionary


【解决方案1】:

一种可能的解决方案是将卡的 ID 保存到变量 (firstId)。如果此变量为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)
}

【讨论】:

  • 你的回答真是神!我能再问一个建议吗(你现在可以救我的命)假设我有一个可以旋转我的卡片 topDown 的功能和一个可以旋转 downTop 的功能。所以如果卡片向下,我调用函数向上旋转,否则如果卡片向上,我调用函数向下旋转现在我找到了我的那对,我该怎么说,第一对,不要再旋转了,继续下一张卡片
  • 您可以将旋转功能放在代码中的必要位置,请参阅我更新的代码中的 cmets
  • 非常感谢您的回答,但我有一些奇怪的行为,例如当且仅当第二张卡等于第一张卡时,我可以旋转第二张卡如果不是,我无法旋转它我更新我的问题你能看一下吗,否则没用 再次感谢
  • 您将卡片旋转两次。在此行之前删除self.firstRotation(card: card)if let firstId = firstId {
  • 我仍然有一些奇怪的行为......旋转在正确的位置和正确的位置吗?我需要在注释之前旋转四行吗// ROTATION UP
猜你喜欢
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2014-11-02
相关资源
最近更新 更多