Ios 如何检测随机图像的名称?
我使用以下代码弹出一个随机图像:Ios 如何检测随机图像的名称?,ios,swift,uiimage,uiswipegesturerecognizer,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.alph
@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分!”如果向下滑动了错误的图像,我希望它说“游戏结束!”我已经有一些代码不起作用,因为它只说“游戏结束!”在我滑动的地方:
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!")
}
将
UIImageView
与UIImage
进行比较永远不会成功。因此,没有办法做到这一点?您可以尝试将图像视图的图像与UIImage
进行比较。图像视图的图像?不,它不起作用:(