How to check if user doesn't write anything in 4 seconds? - ios

I have the next function:
override func textViewDidChange(textView: UITextView) {
super.textViewDidChange(textView)
if textView.text.characters.count == 0 {
print("stopped")
} else {
print("typing")
}
}
So, I want the next:
For example, user is typing something and stopped in the middle of text. I want to check, if user wrote something and stopped in the middle of typing for 4 seconds to run the function stopped().
I did this:
override func textViewDidChange(textView: UITextView) {
super.textViewDidChange(textView)
if textView.text.characters.count == 0 {
print("stopped")
} else {
print("typing")
let timer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "stopped", userInfo: nil, repeats: false)
}
}
func stopped() {
print("stopped typing")
}
but it runs my NSTimer every time. That's not what I want.
How can I run it just one time? For example to check if user didn't write anything in 4 seconds to run stopped(). Just one time.

You need to stop the previous timer with invalidate
var timer
override func textViewDidChange(textView: UITextView) {
super.textViewDidChange(textView)
if textView.text.characters.count == 0 {
print("stopped")
} else {
timer.invalidate()
print("typing")
timer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "stopped", userInfo: nil, repeats: false)
}
}

You'll have to keep a reference on your timer and invalidate it whenever textViewDidChange is called, something like that :
var textViewTimer : NSTimer?
override func textViewDidChange(textView: UITextView) {
super.textViewDidChange(textView)
textViewTimer.invalidate()
if textView.text.characters.count == 0 {
print("stopped")
} else {
print("typing")
textViewTimer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "stopped", userInfo: nil, repeats: false)
}
}
func stopped() {
print("stopped typing")
}

Here is a solution for Swift 5
var textViewTimer: Timer?
override func textViewDidChange(textView: UITextView) {
textViewTimer.invalidate()
print("typing")
textViewTimer = Timer.scheduledTimer(timeInterval: 4.0, target: self, selector: #selector(typingStopped), userInfo: nil, repeats: false)
}
}
#objc func typingStopped() {
print("stopped")
}

Related

Vibrate indefinitely in Swift

How can I make an iPhone vibrate indefinitely? I know I can do this:
while true {
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
Is there any way to play the vibration for a certain number of seconds, or something with a similar result that is cleaner? Also, is there a way to make the vibration more intense?
I think you should use Timer.
var timer: Timer?
var numberRepeat = 0
var maxNumberRepeat = 20
func startTimer() {
//
timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.timerHandler), userInfo: nil, repeats: true)
}
func stopTimer() {
timer?.invalidate()
timer = nil
}
#objc func timerHandler() {
if numberRepeat <= maxNumberRepeat {
numberRepeat += 1
vibrateDevice()
} else {
stopTimer()
}
}
func vibrateDevice() {
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}

how to restart the timer in swift

i am making an application where i am using OTP screen . i worked out with the timer functionality and also able to change the text and show the timer again . my issue is i am not able to restart the timer again. it gets stuck in 00:10.
Here is my following code please Help !!
#IBOutlet weak var butttonName: ButtonDesign!
var countTimer:Timer!
var counter = 10
var restartTimer = false
var isTimerRunning = false
override func viewDidLoad() {
if isTimerRunning == false {
startTimer()}
}
func startTimer() {
self.countTimer = Timer.scheduledTimer(timeInterval: 1 , target: self, selector: #selector(OTPVerificationViewController.changeTitle), userInfo: nil, repeats: true)
isTimerRunning = true
}
#objc func changeTitle()
{
if counter != 0
{
butttonName.setTitle(timeString(time: TimeInterval(counter)), for: .normal)
counter -= 1
butttonName.isEnabled = false
} else {
countTimer.invalidate()
butttonName.setTitle("Resend", for: .normal)
}
}
#IBAction func verifyButton(_ sender: UIButton) {
if butttonName.currentTitle == "Resend" {
print("clicked when name is resend !!!")
if restartTimer == true {
startTimer()
restartTimer = false
}
} else {
print("clicked when it is name is verify")
self.performSegue(withIdentifier: "confirmPassword", sender: self)
}
}
I want to restart the timer whenever the user clicked on "Resend" Button and want to perform segue whenever the user clicked on "Verify" button . both button are the same one i am changing the name on run time
You need to restart the counter too
if butttonName.currentTitle == "Resend" {
counter = 10
// startTimer()
}
Just make the method just like below:
var count = 120
func updateCounter(){
if #available(iOS 10.0, *) {
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (timer) in
if(self.count > 0){
self.lblTime.text = "\(self.count)"
self.count = self.count-1
}else {
self.lblTime.text = "0"
timer.invalidate()
self.btnresend.isHidden = false // in your case you can change the title of the button
}
})
} else {
// Fallback on earlier versions
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.timerMethod), userInfo: nil, repeats: true)
}
}
#objc func timerMethod() {
if(self.count > 0){
self.lblTime.text = "\(self.count)"
self.count = self.count-1
}else {
self.lblTime.text = "0:0"
self.btnresend.isHidden = false
}
}
And from where you want to restart the time just call the update counter method
with set count 120 or you want
self.count = 120

NSTimer invalidate not work

I can not stop the timer NSTimer created to perform secondary operations.
I have read and tried all sorts of possible Web guides but none have worked and the timer continues to run even when the Controller is destroyed. Below is the code in question:
/* Copy banner in a temp array and change order to random value */
private func getBanners(){
let realm = try! Realm()
let banners = self.synchronizer.getAllBanners()?.sorted("ordine")
if let banners = banners {
for banner in banners {
let random = Double.random(0.0, 1.0)
let currentOrderValue = banner.ordine
self.banners.append(banner)
do {
try realm.write({
self.banners.last!.ordine = currentOrderValue + random
})
} catch let error as NSError {
print(error)
}
}
}
}
/*Update index of banner to show */
func updateIndex(timer : NSTimer) {
if self.index+1 < self.banners.count {
for banner in self.banners{
print(banner.name + " \(banner.ordine)")
}
self.index+=1
} else {
self.index = 0
}
self.setImageBanner()
}
/* Set image of banner to show*/
private func setImageBanner() {
if self.banners.count > 0 {
self.bannerImage.hidden = false
let banner = self.banners[self.index]
let file = banner.images[0]
self.synchronizer.loadImageURL(file.link, imageView: self.bannerImage)
} else {
self.bannerImage.hidden = true
}
}
/* Start Timer */
func startTimerForBanners() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.getBanners()
})
self.timer = NSTimer(timeInterval: 5, target: self, selector: #selector(self.updateIndex(_:)), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer!, forMode: NSRunLoopCommonModes)
}
/* Open link on banner click */
func openLink(sender : UITapGestureRecognizer){
if self.index >= 0 && self.index < self.banners.count {
let banner = self.banners[self.index]
print(banner.name)
self.synchronizer.openLink(banner.url)
}
}
func trialLesson(sender : UITapGestureRecognizer){
performSegueWithIdentifier("trial_lesson_segue", sender: nil)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if let timer = self.timer {
timer.invalidate()
}
}
you need to invalidate the timer before starting a new timer. Currently I believe you are calling "startTimerForBanners" many times may be, so many timer thread has been initiated. One approach is to invlidate all timer one by one or just invalidate before starting new one like this
/* Start Timer */
func startTimerForBanners() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.getBanners()
})
**self.timer.invalidate()**
self.timer = NSTimer(timeInterval: 5, target: self, selector: #selector(self.updateIndex(_:)), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer!, forMode: NSRunLoopCommonModes)
}
Are you sure that your controller is really deallocated?
There is one thing you should be always aware about NSTimer: it retains it's target.
I would suggest to:
Start your timer in viewWillAppear
Invalidate and set timer to nil in viewDidDisappear
Also try replacing your start timer code with this (Swift 3):
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.updateIndex(_:)), userInfo: nil, repeats: true)

Timer.scheduledTimer Swift 3 pre-iOS 10 compatibility

I need to schedule a Timer for firing a function every second but I see that in Xcode 8 beta 3 the scheduledTimer is only available for iOS 10.
Is there any alternative for using the timer in iOS 9 or previous versions?
Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in print("Hi!")})
Solved using
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(self.updateTime),
userInfo: nil,
repeats: true)
Run a timer with swift3,
var timer: Timer?
func startTimer() {
if timer == nil {
timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.loop), userInfo: nil, repeats: true)
}
}
func stopTimer() {
if timer != nil {
timer?.invalidate()
timer = nil
}
}
func loop() {
let liveInfoUrl = URL(string: "http://192.168.1.66/api/cloud/app/liveInfo/7777")
let task = URLSession.shared.dataTask(with: liveInfoUrl! as URL) {data, response, error in
guard let data = data, error == nil else { return }
print(String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) ?? "aaaa")
}
task.resume()
}
Release the timer when you not use it.
Once scheduled on a run loop, the timer fires at the specified
interval until it is invalidated. A nonrepeating timer invalidates
itself immediately after it fires. However, for a repeating timer, you
must invalidate the timer object yourself by calling its invalidate()
method.
Here is sample code workable with compatibility:
if #available(iOS 10.0, *) {
Timer.scheduledTimer(withTimeInterval: 15.0, repeats: true){_ in
// Your code is here:
self.myMethod()
}
} else {
Timer.scheduledTimer(timeInterval: 15.0, target: self, selector: #selector(self.myMethod), userInfo: nil, repeats: true)
}
//Your method or function:
// MARK: - Method
#objc func myMethod() {
print("Hi, How are you.")
}
Swift 3
func runCode(in timeInterval:TimeInterval, _ code:#escaping ()->(Void))
{
DispatchQueue.main.asyncAfter(
deadline: .now() + timeInterval,
execute: code)
}
func runCode(at date:Date, _ code:#escaping ()->(Void))
{
let timeInterval = date.timeIntervalSinceNow
runCode(in: timeInterval, code)
}
func test()
{
runCode(at: Date(timeIntervalSinceNow:2))
{
print("Hello")
}
runCode(in: 3.0)
{
print("World)")
}
}
Updated for swift 3:
If you want to use Timer for some delay or any other purpose used below lines of code in your project;
// function defination:
func usedTimerForDelay() {
Timer.scheduledTimer(timeInterval: 0.3,
target: self,
selector: #selector(self.run(_:)),
userInfo: nil,
repeats: false)
}
func run(_ timer: AnyObject) {
print("Do your remaining stuff here...")
}
// function call:
self.usedTimerForDelay()
NOTE:- you can change the time interval as you want.
//Enjoy coding..!
Timer.scheduledTimer
Put it in the main thread.
You can use the following simple shim to provide the new block-based Timers to pre-iOS 10:
class TimerShim {
private var timer: Timer?
private let block: (Timer) -> Void
private init(timeInterval interval: TimeInterval, repeats: Bool, block: #escaping (Timer) -> Void) {
self.block = block
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(timerDidFire), userInfo: nil, repeats: repeats)
}
class func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: #escaping (Timer) -> Void) -> Timer {
return TimerShim(timeInterval: interval, repeats: repeats, block: block).timer!
}
#objc private func timerDidFire() {
block(timer!)
}
}
Usage example:
TimerShim.scheduledTimer(withTimeInterval: 5, repeats: false) { _ in
print("boom!")
}
The correct form is:
Timer.scheduledTimer(withTimeInterval: 2, repeats: false){_ in
"Here your code method"
}

Swift 2 - Timed Actions one second apart?

I'm trying to get output like so:
1 (then a one second delay)
Hello
2 (then a one second delay)
Hello
3 (then a one second delay)
Hello
But instead I get
1
2
3 (then a one second delay)
Hello
Hello
Hello
Here's my for loop invoking the NSTimer
var timer = NSTimer()
for i in 1...3 {
print("\(i)");
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(MainVPScreenViewController.printTest), userInfo: nil, repeats: false)
}
And here's the selector method:
func printTest() {
print("Hello")
}
Thanks in advance for any help you can provide
Try this solution without NSTimer:
var i = 1
func printHello() {
print(i)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
print("Hello")
i +=1
if i <= 3 {
printHello()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
printHello()
}
I need 2 NSTimers to do this, this is my approach
class ViewController: UIViewController {
var i = 1
override func viewDidLoad() {
super.viewDidLoad()
beginPrinting()
}
func beginPrinting() {
var timer2 = NSTimer()
if(i <= 100)
{
timer2 = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.printWithDelay), userInfo: nil, repeats: false)
}
}
func printWithDelay()
{
var timer = NSTimer()
print("\(i)");
i += 1
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.printTest), userInfo: nil, repeats: false)
}
func printTest() {
print("Hello")
beginPrinting()
}
}
Hope this helps you
Use timer with repeat to true. So in your view controller would be like this
var timer = NSTimer()
var counter = 0
var max = 10
let delay = 1 // in second
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(delay, target: self,
selector: #selector(self.printTest), userInfo: nil, repeats: true)
}
func printTest() {
counter += 1
print(counter)
print(hello)
if counter == maxNumber {
timer.invalidate()
}
}
This does it with repeat false, and is set up to be in a playground:
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
#objc class Foo: NSObject {
static var timer = NSTimer()
var i:Int
override init() {
Foo.timer = NSTimer()
i = 1
}
func schedule() {
print("\n\(i)");
i += 1
Foo.timer = NSTimer.scheduledTimerWithTimeInterval(1.0,
target: self,
selector: #selector(printTest),
userInfo: nil,
repeats: false)
}
#objc func printTest() {
print("Hello")
if i < 5 {
schedule()
}
}
}
let bar = Foo()
bar.schedule()

Resources