我无法控制/理解代码流

时间:2015-09-03 11:57:12

标签: ios swift nstimer

在longPressed函数中

,我希望它完成NSTimer中写的initializeTimer()的执行,然后转到validate()。但它似乎没有发生。有人可以理解我的错误,因为我无法做到正确......输出是在代码之后给出的......

我也尝试使用fire()但它只执行一次,我需要它继续在用户需要时重复它... 谢谢....

import UIKit

    class ViewController: UIViewController {
      @IBOutlet weak var progressBar: UIProgressView!
      var choice:Int = 0
      var tim = NSTimer()

    override func viewDidLoad() {
              super.viewDidLoad()
        println("In ViewDidLoad")
        let longPress: UILongPressGestureRecognizer =  UILongPressGestureRecognizer(target: self, action: "longPressed:")
            self.view.addGestureRecognizer(longPress)
    }

    func longPressed(recognizer: UILongPressGestureRecognizer){
            if(recognizer.state == UIGestureRecognizerState.Began){
                println("Long Pressed Begin")
                initializeTimer()
                validate(true, choice: choice)
           }
    }

    func initializeTimer(){
            println("In InitializeTimer")
            progressBar.setProgress(0, animated: true)
            tim = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: "setProgress", userInfo: nil, repeats: true)
        }
    func validate(res: Bool, choice: Int){
            println("In validate ")
    }

    func setProgress(){
       println("In setProgress") 
       tim.invalidate()
    }
    }

输出:

ViewDidLoadLong Pressed Begin
InitializeTimer中 在validate中 在设置进度中

0 个答案:

没有答案