Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Replace NSTimer with CADisplayLink #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions TouchVisualizer/TouchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ final public class TouchView: UIImageView {

// MARK: - Public Variables
internal weak var touch: UITouch?
private weak var timer: Timer?
private var displayLink: CADisplayLink?
private var _config: Configuration
private var previousRatio: CGFloat = 1.0
private var startDate: Date?
private var startTime: CFTimeInterval = 0
private var lastTimeString: String!

public var config: Configuration {
Expand Down Expand Up @@ -59,7 +59,7 @@ final public class TouchView: UIImageView {
}

deinit {
timer?.invalidate()
displayLink?.invalidate()
}

// MARK: - Begin and end touching functions
Expand All @@ -69,12 +69,9 @@ final public class TouchView: UIImageView {
layer.transform = CATransform3DIdentity
previousRatio = 1.0
frame = CGRect(origin: frame.origin, size: _config.defaultSize)
startDate = Date()
timer = Timer.scheduledTimer(timeInterval: 1.0 / 60.0, target: self, selector: #selector(self.update(_:)), userInfo: nil, repeats: true)

RunLoop
.main
.add(timer!, forMode: RunLoop.Mode.common)
startTime = CACurrentMediaTime()
displayLink = CADisplayLink(target: self, selector: #selector(update(_:)))
displayLink?.add(to: .main, forMode: .common)

if _config.showsTimer {
timerLabel.alpha = 1.0
Expand All @@ -86,14 +83,13 @@ final public class TouchView: UIImageView {
}

func endTouch() {
timer?.invalidate()
displayLink?.invalidate()
displayLink = nil
}

// MARK: - Update Functions
@objc internal func update(_ timer: Timer) {
guard let startDate = startDate else { return }

let interval = Date().timeIntervalSince(startDate)
@objc internal func update(_ timer: CADisplayLink) {
let interval = CACurrentMediaTime() - startTime
let timeString = String(format: "%.02f", Float(interval))
timerLabel.text = timeString

Expand Down