#IBAction func BtnClickMe(sender: UIButton) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.CLOCK), userInfo: nil, repeats: true)
}
Here is my clock function when timer is running, stop clock when it reach 0 sec.
#objc func CLOCK(){
sec = sec - 1
lbnTime.text = "\(sec)"
if(sec == 0){
lbnTime.text = "Time Up"
timer.invalidate()
//disable button here
}
}
Add the button as IBOutlet property
and disable it with: button.isEnabled = false
Related
I am making timer. I cannot figure out how to make start button tapped only once as it is starting to count. And at stop button timer.invalidate() does not work
#IBAction func start(_ sender: Any) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}
#IBOutlet weak var stopOutlet: UIButton!
#IBAction func stop(_ sender: Any) {
seconds = 30
label.text = "\(seconds)"
timer.invalidate()
audioPlayer.stop()
}
Just disable the button
#IBAction func start(_ sender: Any) {
stopOutlet.isEnabled = false
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}
Then re-enable when needed:
stopOutlet.isEnabled = true
UIButton extends UIControl. UIControl provides this and alot more functionality to all controls.
this is i seting a timer function, the code like this:
#IBAction func start(_ sender: UIButton) {
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector:#selector(ViewController.action),
userInfo: nil,
repeats: true)
}
#objc func action() {
hoursMinutesSeconds()
if stop == true{
start = false
timer.invalidate()
timer.invalidate()
time = 0
}
}
#IBAction func stop(_ sender: UIButton){
start = false
timer.invalidate()
timer.invalidate()
time = 0
}
but, when i click the stop func, this function not work. mean is the timer not stop. timer stil working…… why? thank you for your time!!
I think you didn't set timer value
timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector:#selector(ViewController.action),
userInfo: nil,
repeats: true)
First you have to declare it as fileprivate as below:
fileprivate var timer = Timer()
#IBAction func start(_ sender: UIButton) {
self.timer = Timer.scheduledTimer(timeInterval: 4,
target: self,
selector: #selector(self.updateTimerForLocation),
userInfo: nil,
repeats: true)
}
#IBAction func stop(_ sender: UIButton) {
timer.invalidate()
}
Ensure that you are invalidating correct instance of Timer.
Like in start function, you didn't assign timer instance to any object, but in stop function,you are using timer vaiable to stop that timer.
Which means you try to invalidate a variable, which never instantiated.
Still try below function to stop timer, after assigning timer variable.
#IBAction func stop(_ sender: UIButton) {
start = false
timer.invalidate()
timer = nil
time = 0
}
Hope this work
you are confusing between variable stop and func stop.
also you dont need 2 parameter to manage stop/start status
--
for your question,
you should retain the timer variable to able to use it in other func
declare a global timer
var timer: Timer!
then
timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector:#selector(ViewController.action),
userInfo: nil,
repeats: true)
now you can invalidate it anywhere
I'm currently learning Swift and iOS programming. I want to make a timer that starts with a long press on the button. At the same time I want to change the UIButton image. I leave button, I want the button revert back. Is this possible?
You absolutely can do this. You have to add LongPressGestureRecognizer:
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(yourAction))
button.addGestureRecognizer(longPress)
And then in your func start the timer and change the button's image:
func yourAction(){
Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: false)
button.setImage(UIImage(named: "yourImage"), for: .normal)
}
func timerAction(){
//Do whatever you want
}
You need to implement your own solution for this, something along the lines
1- User touches down (UIControlEvent touchDown), you start a time that fires every x secs/millisecs
2- Timer will fire the action over and over
3- User touches up UICOntrolEvent touchUp, you cancel your timer
So you bind those 2 events to different functions, and start/kill your timers with appropriate actions
How that helps
I will add a code for demonstration:
#IBOutlet var button: UIButton!
var timer: Timer!
var speedAmmo = 100
#IBAction func buttonDown(sender: AnyObject) {
singleFire()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector:#selector(rapidFire), userInfo: nil, repeats: true)
}
#IBAction func buttonUp(sender: AnyObject) {
timer.invalidate()
}
func singleFire() {
if speedAmmo > 0 {
speedAmmo -= 1
print("bang!")
} else {
print("out of speed ammo, dude!")
timer.invalidate()
}
}
func rapidFire() {
if speedAmmo > 0 {
speedAmmo -= 1
print("bang!")
} else {
print("out of speed ammo, dude!")
timer.invalidate()
}
}
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action:#selector(buttonDown(sender:)), for: .touchDown)
button.addTarget(self, action:#selector(buttonUp(sender:)), for: [.touchUpInside, .touchUpOutside])
}
Please try and let me know!
Ive created a timer in swift to move a UISlider from one end to another again and again when a button is pressed. But I'm always getting a breakpoint at the timer line, although everything should be right.
#IBAction func setSliderValue(_ sender: UIButton){
mytimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
}
func timerAction(){
let Range = slider.maximumValue - slider.minimumValue;
let Increment = Range/100;
let newval = slider.value + Increment;
if(Increment >= slider.maximumValue)
{
slider.setValue(newval, animated: true)
}
else
{
slider.setValue(0, animated: true)
}
}
The check of your function is incorrect.
func timerAction(){
let range = slider.maximumValue - slider.minimumValue
let increment = range/100
let newval = slider.value + increment
if newval <= slider.maximumValue {
slider.setValue(newval, animated: true)
} else {
slider.setValue(slider.minimumValue, animated: true)
}
}
Also, in your event handler, should invalidate the timer (if it's not nil) first before instancing a new one.
Its working now with the following code, though the slider is only moving from left to right until it gets invalidated.
#IBAction func setSliderValue(_ sender: UIButton){
mytimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
/*slider.setValue(100, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)*/
}
func timerAction(){
let Range = slider.maximumValue - slider.minimumValue;
let Increment = Range/100;
let newval = slider.value + Increment;
if(Increment <= slider.maximumValue)
{
slider.setValue(newval, animated: true)
print("The value of the slider is now \(slider.value)")
sliderValue = Int(slider.value)
}
else if (Increment >= slider.minimumValue)
{
slider.setValue(newval, animated: true)
}
}
Hope this helps if someone else needs help on a task "starting a count down timer on a button click".
Timer itself has to be declared inside of the button code and additionally create a Obj-C function to update the timer that will be connected trough the #selector.
class ViewController: UIViewController {
var myTimer = Timer()
var secondsToCount = 100
#IBAction func buttonTapped(_ sender: UIButton) {
myTimer.invalidate()
myTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
#objc func updateTimer() {
secondsToCount -= 1
timerDisplayed.text = String(secondsToCount)
}
}
timerDisplayed is a label that I connected to see the value of the timer on the screen of the app.
SWIFT 5.3 & Xcode 12.0.1
I'm using nstimer to run code that changes text every 60 seconds.
I was wondering if anyone knew of a way to skip an iteration of the 60 seconds. Say a user doesn't want to read the text currently on display, at the moment they still have to wait 60 seconds.
Is there a way to immediately move on to the next 60 second loop at a button tap?
This is what I have so far:
var array : String[]()
var x = 0
#IBAction func playBtnPressed(sender: UIButton)
{
update()
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(PlayVC.update), userInfo: nil, repeats: true)
}
func update()
{
if x < array.count {
let item = array[x]
aLbl.text = array.itemTitle
x += 1
}
}
#IBAction func skipBtnPressed(sender: UIButton)
{
}
Thanks in advance for any guidance you can give me! :)
In your skipBtnPressed() you should first invalidate the timer, update data and then set another timer. Like this
#IBAction func skipBtnPressed(sender: UIButton)
{
timer.invalidate()
update()
timer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: #selector(PlayVC.update), userInfo: nil, repeats: true)
}