如何检测随机UIImage的名称?

时间:2015-06-05 21:58:54

标签: ios swift uiimage uiswipegesturerecognizer

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

@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分!"如果错误的图像被刷下来我想要它说'#34; Game Over!"我已经有一些不起作用的代码,因为它只是说#34; 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 

1 个答案:

答案 0 :(得分:0)

要解决此问题,您可能需要设置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!")
}