2017-04-05 92 views
3

我只是想知道是否被按下UIButton的或不特定的時間間隔的UIButton。 (在我的代碼2秒) 隨着2秒期間,如果按鈕被按下時,如果2不是一些值應爲1。檢測是否被按下一個特定的時間間隔

爲了確認,我做了我下面的代碼。 但unexpected結果快到了,我不知道如何解決它。 幫幫我!

import UIKit 

class ViewController: UIViewController { 
    var gameTimer: Timer! 

@IBAction func btnClick(_ sender: Any) { 
    if let button = sender as? UIButton { 
     if (button.isSelected) { 
      print(" Not Selected"); 
     } else { 
      print(" Selected"); 
     } 
    } 
} 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    gameTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(btnClick(_:)), userInfo: nil, repeats: true) 
} 

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

等待第2秒後應該怎麼辦?你想改變的值是2嗎?即這種情況下的重置點是什麼? –

回答

2

如果你想檢查的UIButton按下與否,你應該處理TouchDown處理程序,改變按鈕的狀態,以在着陸hadnler壓制。 您可以跟蹤ToucUpInside方法來改變按鈕的狀態又不壓。

對於前:

button.addTarget(self, action:#selector(BtnPressed(_:)), for: .touch​Down); 

button.addTarget(self, action:#selector(BtnReleased(_:)), for: .touch​Up​Inside); 


var buttonSelection:Bool = false 

func BtnPressed(_ sender: Any) 
{ 
     button.selected = true 

    } 

func BtnReleased(_ sender: Any) 
{ 
     button.selected = false 

    } 

現在,您可以使用定時器來確認按鈕的狀態如下:

gameTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(IsButtonPressed()), userInfo: nil, repeats: true) 



func BtnPressed() 
{ 

    if (button.isSelected) { 
     print("Button is Pressed"); 
    } else { 
     print("Not Pressed"); 
    } 

    } 
+0

非常感謝。通過使用addTarget語法,我解決了我的問題! – ksh

0

BtnClick是輕敲按鈕的作用下,不應該是其計時器觸發的方法。此外,isSelected並不相關,因爲這是您點擊該按鈕時需要手動設置的內容。 (這是一個按鈕的狀態,而不是操作本身)。

你需要做2點的變化:

1)在btnClick你需要做的:

btnClick.selected = true 

2)計時器應該解僱等方法,讓我們說FUNC checkButtonState(),並在此方法,你需要檢查按鍵的狀態就像你在btnClick

func checkButtonState() { 

    if (button.isSelected) { 
     print(" Not Selected"); 
    } else { 
     print(" Selected"); 
    } 
} 
0

sender as? UIButton做是行不通的,因爲在這種情況下,發件人是你的計時器。如果是某人真正點擊該按鈕,該按鈕將成爲發件人。你應該讓你的按鈕propterty並使用此變量,而不是檢查button.isSelected與否。

/克努特

0

如果要添加選擇器計時器,然後默認會去當計時器不作爲的UIButton的說法。

下面的代碼將不會執行:

if let button = sender as? UIButton 

而是採取布爾變量來檢查的天氣裏,選擇或不選擇

var buttonSelection:Bool = false 

func btnClick(_ sender: Any) 
{ 
     if (buttonSelection == true) 
     { 
      buttonSelection = false; 
      print(" Not Selected"); 
     } else { 
      buttonSelection = true; 
      print(" Selected"); 
     } 
    } 

這將有助於解決您的要求。

0
Put this code in button action. 

    @IBAction func btnClick(_ sender: Any) 
    { 
     if let button = sender as? UIButton 
     { 
      button.isSelected = true 
     } 

    } 

    And change your timer selector 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     gameTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(timerSelector), userInfo: nil, repeats: true) 
    } 

    timer selector will check button status. 

    func timerSelector() 
    { 
     if (button.isSelected) { 
       print(" Selected"); 
      } else { 
       print("Not Selected"); 
      } 
    } 

if your button is pressed in 2 second than button.isseleced become true. 
1

您可以使用下面的代碼。

class ViewController: UIViewController { 
    var gameTimer: Timer! 

    @IBAction func btnClick(_ sender: Any) { 
     let button = UIButton() 
     if (button.isSelected) { 
      print(" Not Selected"); 
     } else { 
      print(" Selected"); 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     gameTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(btnClick(_:)), userInfo: nil, repeats: true) 
    } 

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