在swift中重启测验

时间:2017-02-18 14:38:55

标签: ios arrays swift

我正在建立一个测验。当用户没有生命时,会出现一个带有“重启”的按钮,我希望游戏重置,我创建了一个名为finishButton的IBaction按钮,它将所有标签重新启动到其初始值,但我无法使函数randomQuestion为重新开始,或者使字符串randomQuestionArray重置,或者只是其他任何东西,以便我的测验将从零开始。我尝试了所有,我需要你们的帮助,真的很感激。

以下是代码:

import UIKit
import MapKit
import CoreLocation

class SecoundViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet var mapView: MKMapView!

    @IBOutlet var segmentControl: UISegmentedControl!

    @IBOutlet var questionLabel: UILabel!

    @IBOutlet var button1: UIButton!
    @IBOutlet var button2: UIButton!
    @IBOutlet var button3: UIButton!
    @IBOutlet var button4: UIButton!

    @IBOutlet var nextQuestionOutlet: UIButton!

    @IBOutlet var livesLabel: UILabel!
    @IBOutlet var heartLabel: UIImageView!

    @IBOutlet var finishOutlet: UIButton!

    @IBOutlet var countQuestionsLabel: UILabel!

    var correctAnswer = String()
    var randomQuestionArray:[Int] = [1, 2, 3, 4]

    var livesNumber:Int = 3
    var countQuestions:Int = 1

    var manager = CLLocationManager()

    var latitudeSlovenia:Double = 46.15124099999999
    var longitudeSlovenia:Double = 14.995462999999972

    override func viewDidLoad() {
        super.viewDidLoad()

        button1.layer.cornerRadius = 15
        button2.layer.cornerRadius = 15
        button3.layer.cornerRadius = 15
        button4.layer.cornerRadius = 15

        nextQuestionOutlet.isHidden = true

        countQuestionsLabel.text = "Question: 1/4"

        randomQuestions()

        livesLabel.text = String("Lives: 3")
        finishOutlet.isHidden = true

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func mapType(_ sender: Any) {

        if segmentControl.selectedSegmentIndex == 0 {

            mapView.mapType = MKMapType.standard
        }
        if segmentControl.selectedSegmentIndex == 1 {

            mapView.mapType = MKMapType.satellite
        }
        if segmentControl.selectedSegmentIndex == 2 {

            mapView.mapType = MKMapType.hybrid
        }

    }

    @IBAction func loacteMe(_ sender: Any) {

        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

        mapView.showsUserLocation = true

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let userLocation:CLLocation = locations[0] as CLLocation
        manager.stopUpdatingLocation()

        let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)

        let span =  MKCoordinateSpanMake(0.05, 0.05)

        let region = MKCoordinateRegion(center: location, span: span)

        mapView.setRegion(region, animated: true)

    }

    func disable() {

        button1.isEnabled = false
        button2.isEnabled = false
        button3.isEnabled = false
        button4.isEnabled = false

    }

    func enable() {

        button1.isEnabled = true
        button2.isEnabled = true
        button3.isEnabled = true
        button4.isEnabled = true

    }

    func removeTextColor() {

        button1.setTitleColor(UIColor.white, for: UIControlState.normal)
        button2.setTitleColor(UIColor.white, for: UIControlState.normal)
        button3.setTitleColor(UIColor.white, for: UIControlState.normal)
        button4.setTitleColor(UIColor.white, for: UIControlState.normal)

    }

    func loseALife() {

        livesNumber -= 1

        livesLabel.text = String("Lives: \(livesNumber)")

        if livesNumber < 1 {

            nextQuestionOutlet.isEnabled = false
            disable()
            finishOutlet.isHidden = false
            finishOutlet.setTitle("Restart Quizz", for: UIControlState.normal)
        }

        livesLabel.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)

        UIView.animate(withDuration: 1.0,
                       delay: 0,
                       usingSpringWithDamping: CGFloat(0.20),
                       initialSpringVelocity: CGFloat(6.0),
                       options: UIViewAnimationOptions.allowUserInteraction,
                       animations: {
                        self.livesLabel.transform = CGAffineTransform.identity
        },
                       completion: { Void in()  }
        )

        heartLabel.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)

        UIView.animate(withDuration: 1.0,
                       delay: 0,
                       usingSpringWithDamping: CGFloat(0.20),
                       initialSpringVelocity: CGFloat(6.0),
                       options: UIViewAnimationOptions.allowUserInteraction,
                       animations: {
                        self.heartLabel.transform = CGAffineTransform.identity
        },
                       completion: { Void in()  }
        )

    }

    func didUserWin() {

        if countQuestions == 4 && livesNumber == 3 {

            finishOutlet.isHidden = false
            finishOutlet.setTitle("Congrationlations you know them all !", for: UIControlState.normal)

        }

        if countQuestions == 4 && livesNumber == 2 {
            finishOutlet.isHidden = false
            finishOutlet.setTitle("Bravo, only one mistake", for: UIControlState.normal)
        }

        if countQuestions == 4 && livesNumber == 1 {
            finishOutlet.isHidden = false
            finishOutlet.setTitle("Wow, that was close, remeber the 2 mistakes", for: UIControlState.normal)
        }

    }

    func randomQuestions() {

        let randomIndex = Int(arc4random_uniform(UInt32(randomQuestionArray.count)))

        if randomQuestionArray.count > 0 {

            switch(randomQuestionArray[randomIndex]) {

            case 1:

                let latitude:Double = 64.963051
                let longitude:Double = -19.020835000000034

                let span = MKCoordinateSpanMake(25, 25)
                let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), span: span)

                mapView.setRegion(region, animated: true)

                let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

                let objectAnn = MKPointAnnotation()
                objectAnn.coordinate = pinLocation

                self.mapView.addAnnotation(objectAnn)

                questionLabel.text = "The capital of Iceland is ..."
                button1.setTitle("Copenhagen", for: UIControlState.normal)
                button2.setTitle("Reykiavik", for: UIControlState.normal)
                button3.setTitle("Oslo", for: UIControlState.normal)
                button4.setTitle("Ljubiana", for: UIControlState.normal)

                correctAnswer = "2"

                break
            case 2:

                let latitude:Double = -38.416097
                let longitude:Double = -63.616671999999994

                let span = MKCoordinateSpanMake(25, 25)
                let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), span: span)

                mapView.setRegion(region, animated: true)

                let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

                let objectAnn = MKPointAnnotation()
                objectAnn.coordinate = pinLocation

                self.mapView.addAnnotation(objectAnn)

                questionLabel.text = "The capital of Argentina is ..."
                button1.setTitle("Quito", for: UIControlState.normal)
                button2.setTitle("Arger", for: UIControlState.normal)
                button3.setTitle("Macau", for: UIControlState.normal)
                button4.setTitle("Buenos Aires", for: UIControlState.normal)

                correctAnswer = "4"
                break
            case 3:

                let latitude:Double = 45.943161
                let longitude:Double = 24.966760000000022

                let span = MKCoordinateSpanMake(25, 25)
                let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), span: span)

                mapView.setRegion(region, animated: true)

                let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

                let objectAnn = MKPointAnnotation()
                objectAnn.coordinate = pinLocation

                self.mapView.addAnnotation(objectAnn)

                questionLabel.text = "The capital of Romania is ...?"
                button1.setTitle("Bucuresti", for: UIControlState.normal)
                button2.setTitle("Copenhagen", for: UIControlState.normal)
                button3.setTitle("Macau", for: UIControlState.normal)
                button4.setTitle("Oslo", for: UIControlState.normal)

                correctAnswer = "1"
                break
            case 4:

                let latitude:Double = -40.90055699999999
                let longitude:Double = 174.88597100000004

                let span = MKCoordinateSpanMake(25, 25)
                let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude), span: span)

                mapView.setRegion(region, animated: true)

                let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

                let objectAnn = MKPointAnnotation()
                objectAnn.coordinate = pinLocation

                self.mapView.addAnnotation(objectAnn)

                questionLabel.text = "The capital of Neew zeeland is ...?"
                button1.setTitle("Sydney", for: UIControlState.normal)
                button2.setTitle("Wellington", for: UIControlState.normal)
                button3.setTitle("Cambera", for: UIControlState.normal)
                button4.setTitle("Rdasdsadas", for: UIControlState.normal)

                correctAnswer = "2"
                break

            default:
                break

            }

            randomQuestionArray.remove(at: randomIndex)

        }

        /*if randomQuestionArray.count == 0 && randomQuestionArray.count < 0 {
         finishLabel.text = "we arrived at 1"
         nextQuestionOutlet.isEnabled = false
         disable()
         }*/
    }

    @IBAction func directions(_ sender: Any) {
    }

    @IBAction func button1Action(_ sender: Any) {

        disable()
        nextQuestionOutlet.isHidden = false
        didUserWin()

        if(correctAnswer == "1") {

            button1.setTitleColor(UIColor.green, for: UIControlState.normal)
        } else {
            button1.setTitleColor(UIColor.red, for: UIControlState.normal)
            loseALife()

            button1.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)

            UIView.animate(withDuration: 1.0,
                           delay: 0,
                           usingSpringWithDamping: CGFloat(0.20),
                           initialSpringVelocity: CGFloat(6.0),
                           options: UIViewAnimationOptions.allowUserInteraction,
                           animations: {
                            self.button1.transform = CGAffineTransform.identity
            },
                           completion: { Void in()  }
            )

        }
    }

    @IBAction func button2Action(_ sender: Any) {

        disable()
        nextQuestionOutlet.isHidden = false
        didUserWin()

        if(correctAnswer == "2") {

            button2.setTitleColor(UIColor.green, for: UIControlState.normal)

        } else {
            button2.setTitleColor(UIColor.red, for: UIControlState.normal)
            loseALife()

            button2.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)

            UIView.animate(withDuration: 1.0,
                           delay: 0,
                           usingSpringWithDamping: CGFloat(0.20),
                           initialSpringVelocity: CGFloat(6.0),
                           options: UIViewAnimationOptions.allowUserInteraction,
                           animations: {
                            self.button2.transform = CGAffineTransform.identity
            },
                           completion: { Void in()  }
            )

        }
    }

    @IBAction func button3Action(_ sender: Any) {

        disable()
        nextQuestionOutlet.isHidden = false
        didUserWin()

        if(correctAnswer == "3") {
            button3.setTitleColor(UIColor.green, for: UIControlState.normal)

        }else {
            button3.setTitleColor(UIColor.red, for: UIControlState.normal)
            loseALife()

            button3.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)

            UIView.animate(withDuration: 1.0,
                           delay: 0,
                           usingSpringWithDamping: CGFloat(0.20),
                           initialSpringVelocity: CGFloat(6.0),
                           options: UIViewAnimationOptions.allowUserInteraction,
                           animations: {
                            self.button3.transform = CGAffineTransform.identity
            },
                           completion: { Void in()  }
            )

        }
    }

    @IBAction func button4Action(_ sender: Any) {

        disable()
        nextQuestionOutlet.isHidden = false
        didUserWin()

        if(correctAnswer == "4") {
            button4.setTitleColor(UIColor.green, for: UIControlState.normal)

        }else {
            button4.setTitleColor(UIColor.red, for: UIControlState.normal)
            loseALife()

            button4.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)

            UIView.animate(withDuration: 1.0,
                           delay: 0,
                           usingSpringWithDamping: CGFloat(0.20),
                           initialSpringVelocity: CGFloat(6.0),
                           options: UIViewAnimationOptions.allowUserInteraction,
                           animations: {
                            self.button4.transform = CGAffineTransform.identity
            },
                           completion: { Void in()  }
            )

        }
    }

    @IBAction func nextQuestionAction(_ sender: Any) {

        randomQuestions()
        enable()
        removeTextColor()
        nextQuestionOutlet.isHidden = true


        countQuestions += 1
        countQuestionsLabel.text = String("Question: \(countQuestions)/4")
    }

    @IBAction func finishButton(_ sender: Any) {

        livesNumber = 3
        countQuestions = 1

        livesLabel.text = String("Lives: \(livesNumber)")
        countQuestionsLabel.text = String("Question: \(countQuestions)/4")

        enable()
        removeTextColor()
        finishOutlet.isHidden = true

    }
}

1 个答案:

答案 0 :(得分:1)

randomQuestionArray,您需要重新初始化randomQuestionArray = [1, 2, 3, 4]

Intent resumeIntent = new Intent (this, Class.forName(getPackageName() + resumeName);
startActivity(resumeIntent);