簡體   English   中英

整數值持續存在

[英]Int value Persist

當我的touchbegan方法在那里時,我想實施longpressgesture。 我正在使用NSTimer並在counter變為3時記錄該計數器。 我意識到那是長按。 但是當我釋放按鈕然后再次按下mycounter時,保持先前的值並在先前的值上增加。 請幫助任何幫助。

 var counter : Int = 0
  var timer :NSTimer?

 override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        timer = NSTimer()
        counter == 0;
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)

    }

    func updateCounter() {
       print(counter++)
        if (counter > 3){
            timer!.invalidate()
            timer = nil;
        }
    }
 override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        timer!.invalidate()
          timer = nil;
        counter == 0;

    }
  override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {



        if (counter>=3){
            print("hello")

        }

    }

那是經典方程== vs.assign =運算符的混淆

timer = NSTimer() // this line is actually not needed
counter = 0

...

timer?.invalidate() // better use question mark to avoid a potential crash
timer = nil
counter = 0

並刪除所有分號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM