ios - Swift:当对象在一个区域内时运行代码

标签 ios xcode swift

我正在制作一个真假游戏,我们必须确定逻辑表达式是真还是假。这是一个单击游戏。如果我们单击,则当前的 true 条件变为 false,反之亦然。一系列逻辑表达式会从屏幕上弹出,当它到达区域时,当前条件必须等于逻辑表达式才能获得分数。为了验证真值,它检查真值的图像。我试过一个案例,它是“真实的和真实的”并且当前条件是真实的,这是真实的。但是,当它穿过该区域时,分数并没有增加。我哪里出错了?

@IBAction func button_clicked(sender: UIButton) {
    if (truth_click == true) {
        truth_click = false
        self.truth_button.image = UIImage(named: "false_button")
    }
    else if (truth_click == false) {
        truth_click = true
        self.truth_button.image = UIImage(named: "true_button")
    }
}

func check_truth() {
    if (truth_click == true) {
        //when it reaches the zone
        if (left_truth.center.y > 330 ) {

            //if true and true
            if ((left_truth.image == UIImage(named: "true")) && (symbol.image == UIImage(named: "and")) && (right_truth.image == UIImage(named: "true"))) {
                self.score += 1

            }
            //if true and false

            //if false and true

            //if false and false

            self.score_label.text = String(self.score)
        }
    }

    if (truth_click == false) {
        //when it reaches the zone
        if (left_truth.center.y > 330 ) {

            //if true and false
            if ((left_truth.image == UIImage(named: "true")) && (symbol.image == UIImage(named: "and")) && (right_truth.image == UIImage(named: "false")))  {
                self.score += 1

            }
            //if true and true

            //if false and true

            //if false and false

            self.score_label.text = String(self.score)
        }
    }
}

最佳答案

部分问题是针对 UIImage(named:...) 的新实例测试 *_truth.image。 UIImage 是一个对象。实例化一个新的 UIImage 创建一个新对象。具有相同内容的两个对象不相等......没有额外的工作。 enter image description here

关于ios - Swift:当对象在一个区域内时运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35546246/

相关文章:

ios - 如何获得 TableView 的正确索引

iphone - 如何检测类的属性何时更改

objective-c - 滑动 Custome UITableViewCell/Custom UITableViewController 时内存泄漏

swift - 如何在 Swift 中累积旋转图像? (Xcode 8)

swift - 为什么String.subscript(_ :) require the types `String.Index` and `Int` to be equal when there is no `Int` involved?

ios - 自动布局:无法同时满足约束

swift - 如何创建一个快速的通用可变参数函数?

ios - 在 Storyboard xcode 7 中为具有不同宽度的两个 View 设置常量

ios - 点击 `Add Tags` 选项会关闭 UIDocumentInteractionController 突然启动的底层模态视图

ios - 滑动以使用 Segue 编辑详细信息