使用按钮循环遍历代码块并通过swift 2中的数组递增

时间:2015-10-16 14:02:14

标签: swift2 ios9 xcode7

我正在尝试制作一个使用estimote信标的简单寻宝应用程序(我有3个信标)。所以我目前有一个带有按钮的主屏幕开始搜索;按下按钮时,下一个视图控制器是第一个线索。当设备在...附近时接近第一个信标,屏幕改变以指示用户越来越近。当设备处于紧邻状态时,会出现一个带有文本字段的问题并且保存问题'按钮。 (所有这些都在一个视图控制器中完成,使用代码来确定显示哪些项目以及何时显示)。用户回答问题,然后当他们点击“保存问题”时,他们的答案会保存到表格视图中,并显示线索2。 (我还没有完成任何保存)

我的目标是再次循环。因此,不是为每个轮次创建一个新的视图控制器。线索 - >越来越接近 - >问题,我想像以前一样循环回相同的视图控制器 - 如果我获得更多信标,我希望将来能够添加更多问题/线索,所以我&# 39;我试图找到最有效的方法(我希望这是有意义的!)。我觉得我需要在某个地方使用for循环,但我无法弄清楚在哪里。

let questionTitleArray: [String] = ["Question One", "Quesiton Two", "Question Three"]

let clueTitleArray: [String] = ["Clue One", "Clue Two", "Clue Three"]

let questions = [
    11711: "Emporer and King are types of which flightless bird?",
    41848: "What illuminated word appears on the roof of a London cab?",
    24952: "Name the fictional boxer played by Sylvester Stallone"]

let clues: [String] = ["I'm your first clue", "I'm your second clue", "I'm your third clue"]

let questionsArray: [String] = ["Emporer and King are types of which flightless bird?", "What illuminated word appears on the roof of a London cab?", "Name the fictional boxer played by Sylvester Stallone"]

var temperatureImage: [String] = ["fireplace-hi.png"]

let temperatureArray: [String] = ["Getting warmer...", "Getting colder...", "Freezing!"]

let minorValuesArray:[Int] = [11711, 41848, 24952]


//var i = 0

override func viewDidLoad() {
    super.viewDidLoad()
    self.image.alpha = 0
    self.textBox.alpha = 0
    self.button.alpha = 0

    var i = 0

    questionLabel.text = clueTitleArray[i]

    question.text = clues[i]

    locationManager.delegate = self

    if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse)
    {
       locationManager.requestWhenInUseAuthorization()
    }
    locationManager.startRangingBeaconsInRegion(region)



}



func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
    print(beacons)



    let knownBeacons = beacons.filter
        {
            $0.proximity != CLProximity.Unknown
        }

    if (knownBeacons.count > 0)
    {

        let closestBeacon = knownBeacons[0] as CLBeacon

        //insert a for loop
        var i = 0

        //for var i = 0; i < minorValuesArray.count; i++
        //while i < minorValuesArray.count

        if closestBeacon.minor.integerValue == minorValuesArray[i] && closestBeacon.proximity == CLProximity.Near //if the beacon is purple (clue1) and proximity is near, hide the labels and just show image
        {

            self.questionLabel.text = temperatureArray[0]
            self.button.alpha = 0
            self.textBox.alpha = 0
            self.image.alpha = 1
            self.question.alpha = 0



        }

            else if closestBeacon.minor.integerValue == minorValuesArray[0] && closestBeacon.proximity == CLProximity.Immediate //if the beacon is purple and proximity is immediate (user is in the right place), show aa success message and the next question.  Stop ranging so the screen doesn't flicker back to warm.
            {
                self.image.alpha = 0
                self.questionLabel.alpha = 1
                self.question.alpha = 1


                self.questionLabel.text = self.questionTitleArray[i]

                self.question.text = self.questionsArray[i]

                self.textBox.alpha = 1

                self.button.alpha = 1

                locationManager.stopRangingBeaconsInRegion(region)

            }




    }

    }

1 个答案:

答案 0 :(得分:0)

在灯塔项目,也许可以帮助你

import UIKit
import CoreLocation
import MediaPlayer

class OneViewController: UIViewController {
//定义答对图像开始
var goodImg = UIImageView()

//定义答对图像结束
var currentQuestion = 0

let allQuestions = [
    ["ベートーヴェンは何年に生まれたでしょうか","1770年","1868年","1776 年"],
    ["トランペットを吹くために注意した方がいいことは何でしょうか?","マウスピースをくわえる","よく聞こえるように人の近くで吹く","手の形をテニスボールを持っているようにする"],
    ["NaHCO3は何の化学式でしょうか?","酢","重奏","溶岩"]
]

let rightAnswer = [1]

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.
    let backImageView = UIImageView()
    backImageView.frame = CGRectMake(0, 0, 1024, 768)
    let img = UIImage(named: "mainBgImg.png")
    backImageView.image = img
    self.view.addSubview(backImageView)


    let questionLabel = UILabel()
    questionLabel.frame = CGRectMake(595, 265, 370, 30)
    questionLabel.text = allQuestions[0][0]
    questionLabel.textColor = UIColor.whiteColor()
    questionLabel.font = UIFont.systemFontOfSize(18)
    questionLabel.textAlignment = .Center
    self.view.addSubview(questionLabel)

    //在主页面中添加按钮并且设置位置
    let questionBtnA = UIButton()
    questionBtnA.frame = CGRectMake(595, 310, 370, 68)
    questionBtnA.setImage(UIImage(named: "mainBtn"), forState: .Normal)
    self.view.addSubview(questionBtnA)
    questionBtnA.addTarget(self, action: "rightResult:", forControlEvents: .TouchUpInside)
    questionBtnA.tag = 1

    let questionBtnB = UIButton()
    questionBtnB.frame = CGRectMake(595, 400, 370, 68)
    questionBtnB.setImage(UIImage(named: "mainBtn"), forState: .Normal)
    self.view.addSubview(questionBtnB)
    questionBtnB.addTarget(self, action: "rightResult:", forControlEvents: .TouchUpInside)
    questionBtnB.tag = 2

    let questionBtnC = UIButton()
    questionBtnC.frame = CGRectMake(595, 490, 370, 68)
    questionBtnC.setImage(UIImage(named: "mainBtn"), forState: .Normal)
    self.view.addSubview(questionBtnC)
    questionBtnC.addTarget(self, action: "rightResult:", forControlEvents: .TouchUpInside)
    questionBtnC.tag = 3

    //在住页面中添加按钮标签并设置位置
    let buttonLabel1 = UILabel()
    buttonLabel1.frame = CGRectMake(595, 310, 370, 68)
    buttonLabel1.textAlignment = .Center
    buttonLabel1.text = allQuestions[0][1]
    buttonLabel1.font = UIFont.systemFontOfSize(20)
    self.view.addSubview(buttonLabel1)

    let buttonLabel2 = UILabel()
    buttonLabel2.frame = CGRectMake(595, 400, 370, 68)
    buttonLabel2.textAlignment = .Center
    buttonLabel2.text = allQuestions[0][2]
    buttonLabel2.font = UIFont.systemFontOfSize(20)
    self.view.addSubview(buttonLabel2)

    let buttonLabel3 = UILabel()
    buttonLabel3.frame = CGRectMake(595, 490, 370, 68)
    buttonLabel3.textAlignment = .Center
    buttonLabel3.text = allQuestions[0][3]
    buttonLabel3.font = UIFont.systemFontOfSize(20)
    self.view.addSubview(buttonLabel3)



}

func doneButtonClick(sender:NSNotification?){
    //self.dismissMoviePlayerViewControllerAnimated()
    self.dismissViewControllerAnimated(true, completion: nil)
    print("aa")
}


func rightResult(sender:UIButton){
    if sender.tag == rightAnswer[currentQuestion]{
        let imagesccuss = UIButton()
        imagesccuss.frame = CGRectMake(555, 230, 450, 370)
        imagesccuss.setImage(UIImage(named: "yes"), forState: .Normal)
        self.view.addSubview(imagesccuss)
        //答对图案开始
        goodImg.image = UIImage(named: "good.png")
        goodImg.frame = CGRectMake(380, 605, 50, 40)
        self.view.addSubview(goodImg)

        UIImageView.animateWithDuration(2, delay: 0,usingSpringWithDamping:0.5,initialSpringVelocity:0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.goodImg.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(2, 2), CGAffineTransformMakeTranslation(250, 50))
            }, completion: nil)

        //答对图案结束
        imagesccuss.addTarget(self, action:"turnBack",forControlEvents:.TouchUpInside)

    }else{
        let imagesccuss = UIButton()
        imagesccuss.frame = CGRectMake(555, 230, 450, 370)
        imagesccuss.setImage(UIImage(named: "error"), forState: .Normal)
        self.view.addSubview(imagesccuss)
        imagesccuss.addTarget(self, action:"errorBack",forControlEvents:.TouchUpInside)
    }
}

func turnBack() {
    let avc = messagesTwoViewController()
    self.presentViewController(avc, animated: true, completion: nil)
    //self.dismissViewControllerAnimated(true, completion:nil)
}



func errorBack() {
    let vc = OneViewController()
    self.presentViewController(vc, animated: false, completion: nil)
    //self.dismissViewControllerAnimated(true, completion:nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidDisappear(animated: Bool) {
}

}