如何检测随机 UIImage 的名称?

How do I detect the name of a random UIImage?

我使用此代码随机弹出一张图片:

@IBOutlet weak var imageView: UIImageView!

@IBAction func startButton(sender: AnyObject) {

    UIView.animateWithDuration(0.5, delay:2, options:UIViewAnimationOptions.TransitionFlipFromTop, animations: {
        self.imageView.alpha = 2
        }, completion: { finished in
            self.imageView.image = UIImage(named: "gameBall\(arc4random_uniform(12) + 1).png")      
    })
}

当用户向下滑动时,我想检测图像的名称,然后如果向下滑动正确的图像,我希望它说“1 分!”如果向下滑动错误的图像,我希望它说 "Game Over!" 我已经有一些代码不起作用,因为它只在我滑动的地方显示 "Game Over!":

func respondToSwipeGesture(sender: UISwipeGestureRecognizer) {
        switch sender.direction {
        case UISwipeGestureRecognizerDirection.Down:
            if imageView == UIImage(named: "gameBall2.png"){
                println("1 point!")
            }else{
                println("Game Over!")
            }
        default:
            break
        }
    }
override func viewDidLoad() {
        super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down
    self.view.addGestureRecognizer(swipeDown)
    self.view.userInteractionEnabled = true 

要解决这个问题,您可能需要设置 imageView 的标签。

let randomNumber = Int(arc4random_uniform(12) + 1)
self.imageView.image = UIImage(named: "gameBall\(randomNumber).png")
self.imageView.tag = randomNumber

现在您可以比较 if 条件中的标签了。

if self.imageView.tag == 2 {
    println("1 point!")
} else {
    println("Game Over!")
}