Detecting if a UIButton is pressed at a specific time interval
I just want to know if a button is pressed UIButton
or not at a specific time interval. (2 seconds in my code) With a 2 second period, some value should be 1 if the button is pressed, 2 if not.
To test this, I made my code below. But the results are unexpected
getting closer and I have no idea how I can fix this. Help me!
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.
}
}
If you want to check if the UIButton is pressed or not, you have to handle the TouchDown Handler and change the state of the button pressed in the touchDown hadnler. You can watch the ToucUpInside method to change the state of the button so that it is not pressed again.
For ex:
button.addTarget(self, action:#selector(BtnPressed(_:)), for: .touchDown);
button.addTarget(self, action:#selector(BtnReleased(_:)), for: .touchUpInside);
var buttonSelection:Bool = false
func BtnPressed(_ sender: Any)
{
button.selected = true
}
func BtnReleased(_ sender: Any)
{
button.selected = false
}
Now you can use a timer to check the state of the button like this:
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");
}
}
You can use the following code.
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.
}
}
BtnClick is the action of the button being tapped, not intended as a method, that fires the timer. Also, isSelected doesn't matter as it has to be manually set when the button is clicked. (This is the state of the button, not the action itself.)
You need to make 2 changes:
1) In btnClick you need to do:
btnClick.selected = true
2) The timer should start another method, say func checkButtonState () and in this method you need to check the state of the button as you did in btnClick
func checkButtonState() {
if (button.isSelected) {
print(" Not Selected");
} else {
print(" Selected");
}
}
sender as? UIButton
won't work as the sender is your timer in this case. If someone pressed the button, the button would be the sender. You should probably make your button propterty and use that variable instead to check if button.isSelected
true .
/Whip
If you add a selector to Timer then the default argument will appear as a timer not as a UIButton.
Below code won't execute:
if let button = sender as? UIButton
Instead, grab the BOOL variable to check whether the weather is selected or not
var buttonSelection:Bool = false
func btnClick(_ sender: Any)
{
if (buttonSelection == true)
{
buttonSelection = false;
print(" Not Selected");
} else {
buttonSelection = true;
print(" Selected");
}
}
This will help you solve your requirements.
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.