import UIKit
class ViewController: UIViewController {
var timerCount = 5
var timerRunning = false
var timer = NSTimer()
func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
let c = count(list)
for i in 0..<(c - 1) {
let j = Int(arc4random_uniform(UInt32(c - i))) + i
swap(&list[i], &list[j])
}
return list
}
func counting(){
timerCount -= 1
timerLabel.text = "\(timerCount)"
if timerLabel.text == "0"{
if timerLabel.text == "0"{
timerLabel.text = ""
self.youFailedView.image = UIImage(named: "youFailed")
}
}
}
var backRoundRand = arc4random_uniform(4) + 1
@IBOutlet weak var backRound: UIImageView!
@IBOutlet weak var youFailedView: UIImageView!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var sign1: UIImageView!
@IBOutlet weak var sign2: UIImageView!
@IBOutlet weak var sign3: UIImageView!
@IBOutlet weak var sign4: UIImageView!
@IBOutlet weak var sign5: UIImageView!
@IBOutlet weak var color1: UIImageView!
@IBOutlet weak var color2: UIImageView!
@IBOutlet weak var color3: UIImageView!
@IBOutlet weak var color4: UIImageView!
@IBOutlet weak var color5: UIImageView!
@IBOutlet weak var blueButton: UIButton!
@IBOutlet weak var purpleButton: UIButton!
@IBOutlet weak var orangeButton: UIButton!
@IBOutlet weak var yellowButton: UIButton!
@IBOutlet weak var redButton: UIButton!
override func viewDidLoad() {
var gameViewStillRunning = true
var sign1RandNum = arc4random_uniform(3) + 1
var sign2RandNum = arc4random_uniform(4) + 1
var sign3RandNum = arc4random_uniform(4) + 1
var sign4RandNum = arc4random_uniform(4) + 1
var sign5RandNum = arc4random_uniform(4) + 1
var colorArray = ["orangeBrick", "blueBrick", "yellowBrick", "redBrick", "purpleBrick"]
var shuffledColorArray: Array = shuffle(colorArray)
self.color1.image = UIImage(named: shuffledColorArray[0])
self.color2.image = UIImage(named: shuffledColorArray[1])
self.color3.image = UIImage(named: shuffledColorArray[2])
self.color4.image = UIImage(named: shuffledColorArray[3])
self.color5.image = UIImage(named: shuffledColorArray[4])
var correctAnswer1 = ""
var correctAnswer2 = ""
var correctAnswer3 = ""
var correctAnswer4 = ""
var correctAnswer5 = ""
/*switch backRoundRand{
case 1:
self.backRound.image = UIImage(named: "redBack")
case 2:
self.backRound.image = UIImage(named: "greenBack")
case 3:
self.backRound.image = UIImage(named: "orangeBack")
case 5:
self.backRound.image = UIImage(named: "purpleBack")
default:
break
}*/
switch sign1RandNum{
case 1:
self.sign1.image = UIImage(named: "blank")
correctAnswer1 = shuffledColorArray[0]
case 2:
self.sign1.image = UIImage(named: "forward")
correctAnswer1 = shuffledColorArray[1]
case 3:
self.sign1.image = UIImage(named: "fastForward")
correctAnswer1 = shuffledColorArray[2]
case 4:
self.sign1.image = UIImage(named: "backSpace")
correctAnswer1 = shuffledColorArray[4]
default:
break
}
switch sign2RandNum{
case 1:
self.sign2.image = UIImage(named: "blank")
correctAnswer2 = shuffledColorArray[1]
case 2:
self.sign2.image = UIImage(named: "forward")
correctAnswer2 = shuffledColorArray[2]
case 3:
self.sign2.image = UIImage(named: "fastForward")
correctAnswer2 = shuffledColorArray[3]
case 4:
self.sign2.image = UIImage(named: "backSpace")
correctAnswer2 = shuffledColorArray[0]
default:
break
}
switch sign3RandNum{
case 1:
self.sign3.image = UIImage(named: "blank")
correctAnswer3 = shuffledColorArray[2]
case 2:
self.sign3.image = UIImage(named: "forward")
correctAnswer3 = shuffledColorArray[3]
case 3:
self.sign3.image = UIImage(named: "fastForward")
correctAnswer3 = shuffledColorArray[4]
case 4:
self.sign3.image = UIImage(named: "backSpace")
correctAnswer3 = shuffledColorArray[1]
default:
break
}
switch sign4RandNum{
case 1:
self.sign4.image = UIImage(named: "blank")
correctAnswer4 = shuffledColorArray[3]
case 2:
self.sign4.image = UIImage(named: "forward")
correctAnswer4 = shuffledColorArray[4]
case 3:
self.sign4.image = UIImage(named: "fastForward")
correctAnswer4 = shuffledColorArray[0]
case 4:
self.sign4.image = UIImage(named: "backSpace")
correctAnswer4 = shuffledColorArray[2]
default:
break
}
switch sign5RandNum{
case 1:
self.sign5.image = UIImage(named: "blank")
correctAnswer5 = shuffledColorArray[4]
case 2:
self.sign5.image = UIImage(named: "forward")
correctAnswer5 = shuffledColorArray[0]
case 3:
self.sign5.image = UIImage(named: "fastForward")
correctAnswer5 = shuffledColorArray[1]
case 4:
self.sign5.image = UIImage(named: "backSpace")
correctAnswer5 = shuffledColorArray[3]
default:
break
}
if timerRunning == false{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("counting"), userInfo: nil, repeats: true)
timerRunning = true
}
if userAnswer != correctAnswer1{
self.youFailedView.image = UIImage(named: "youFailed")
}
}
var userAnswer = ""
@IBAction func blueButtonPressed(sender: UIButton) {
userAnswer = "blueBrick"
}
@IBAction func purpleButtonPressed(sender: UIButton) {
userAnswer = "purpleBrick"
}
@IBAction func orangeButtonPressed(sender: UIButton) {
userAnswer = "orangeBrick"
}
@IBAction func yellowButtonPressed(sender: UIButton) {
userAnswer = "yellowBrick"
}
@IBAction func redButtonPressed(sender: UIButton) {
userAnswer = "redBrick"
}
}
我不确定当我的播放按钮被移动到这个场景时,为什么这个代码会停止程序。播放按钮在我的第一个视图上,我在故事板上连接了一个segue,在按下时直接进入该视图控制器。但是,一旦按下播放按钮,就不会发生任何事情,也不会移动到此场景。不知道为什么会这样做,没有无限循环或我看到的原因。有什么建议?此外,如果有人知道如何简化一些很好的代码,我是一个初学者和新手。
答案 0 :(得分:0)
您可以对应用程序进行概要分析,以查看代码的哪一部分需要很长时间才能执行,这将有助于缩小范围。按住运行按钮并选择配置文件。
从出现的菜单中点击“Time Profiler”。
点击录制按钮,您将看到应用程序显示。
遇到缓慢后,点击探查器中的停止按钮。突出显示执行时间消耗的峰值。
然后,您将能够看到被调用的方法以及在此期间需要执行的时间。
展开该屏幕上的方法以确定代码的哪个部分导致缓慢,然后可以对其进行优化以使其更快或将其移动到后台线程中。