I have this code that is suppossed to make my phone vibrate constantly. but it stops after 1 vibration. Is there something wrong with my timer?
var timer: Timer?
#IBAction func button1(_ sender: UIButton) {
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: Selector(("doaction")), userInfo: nil, repeats: true)
}
What did I do wrong? help? I am also using xcode with swift. and have an iphone 7 plus if that matters.
The timer does not stop vibrating after one round. The timer never vibrates.
What the timer does is to call the doaction method repeatedly. But the doaction method does not perform any vibration. Therefore the only vibration is the single one in button1.
The vibration is only happening when you tap the button, you need the vibration to happen in your "doaction" method:
var timer: Timer?
#IBAction func button1(_ sender: UIButton) {
doaction()
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: Selector(("doaction")), userInfo: nil, repeats: true)
}
func doaction() {
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
}
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 made an app in xcode that vibrates forever with a button, and stops with a button, but when I test it on my iphone it doesn't vibrate. Help?
var timer: Timer?
#IBAction func button1(_ sender: UIButton) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval: 0.0, target: self, selector: Selector(("doaction")), userInfo: nil, repeats: true)
}
#IBAction func button2(_ sender: UIButton) {
timer?.invalidate()
timer = nil
}
I am also using an iphone 7 plus, if that makes a difference.
Replace AudioServicesPlaySystemSound with AudioServicesPlayAlertSound. Also, make sure that you are importing AudioToolbox.
If you want to use AudioServicesPlaySystemSound, you have to do it as follows
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
I want to access myTimer variable from startTimer() function inside my backBtnPressed() function. Basically i want to add this code myTimer.invalidate() inside my backBtnPressed() function. How can i achieve that?
func startTimer() {
var myTimer = Timer.scheduledTimer(timeInterval: 3.0,
target: self,
selector: #selector(scrollToNextCell),
userInfo: nil,
repeats: true)
}
#IBAction func backBtnPressed(_ sender: UIButton) {
audioPlayer.stop()
}
As it stands now, you cannot access myTimer variable outside startTimer(), because it is outside the scope. For that, you need to declare myTimer as a Class variable. Them, you need to initialize it, as you are doing, and them you can access whatever you want inside the Class. Also don't forget to call startTimer, or it will return nil.
It looks more or less like this:
class YourViewController: ViewController {
var myTimer: Timer?
//some of your functions here
//...
override func viewDidLoad() {
super.viewDidLoad()
//...
startTimer()
}
func startTimer() {
myTimer = Timer.scheduledTimer(timeInterval: 3.0,
target: self,
selector: #selector(yourFunction),
userInfo: nil,
repeats: true)
}
#IBAction func backBtnPressed(_ sender: UIButton) {
//do whatever you want
myTimer.invalidate()
}
}
Simple make your timer a property:
class MyClass {
var timer: Timer?
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(scrollToNextCell), userInfo: nil, repeats: true)
}
#IBAction func backBtnPressed(_ sender: UIButton) {
audioPlayer.stop()
timer.//do something with timer
}
}
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)
}