Skip to content

Repeating at a certain time

   

Repeat every three seconds using Time.
I used it to create an app that functions like a clock.

import UIKit
class ViewController: UIViewController {
var timer: Timer!
override func viewDidLoad() {
super.viewDidLoad()
//3秒ごとに繰り返す、repeat every 3 seconds
timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
timer.fire()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
timer.invalidate()
}
@objc func update(tm: Timer) {
//この関数を繰り返す、repeat this function
print("Hello SwiSwiSwift")
}
}

  1. Create a collection view with custom layouts
  2. UDP communication in Swift and sending a string
  3. Removing @objc inference warnings when migrating from Swift3 to Swift4
  4. Pinch to zoom in and out on an image with UIScrollView
  5. Removing the back button in UINavigationController.
  6. Execute after ○ seconds, execute every second
  7. Setting a header in UITableView