gpt4 book ai didi

ios - 如何检测随机 UIImage 的名称?

转载 作者:行者123 更新时间:2023-11-30 14:19:28 25 4
gpt4 key购买 nike

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

@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!")
}

关于ios - 如何检测随机 UIImage 的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30676445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com