Skip to content

timdonnelly/DisplayLink

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DisplayLink

A simplified DisplayLink abstraction for all platforms (including iOS, tvOS, watchOS, macOS, Linux).

public final class DisplayLink : Publisher {
    public typealias Output = Frame
    public typealias Failure = Never
}

extension DisplayLink {
    public struct Frame {
        public var timestamp: TimeInterval
        public var duration: TimeInterval
    }
}
Platform Implementation
iOS, tvOS CADisplayLink
macOS CVDisplayLink
watchOS, Linux Timer

Includes a Combine publisher with SwiftUI integration for CADisplayLink.

SwiftUI does not currently provide any API to perform actions on a per-frame basis. This tiny library simplifies the work of bridging between CADisplayLink and SwiftUI:

import DisplayLink

struct MyView: View {

    @State var offset: CGFloat = 0.0

    var body: some View {
        Color
            .red
            .frame(width: 40, height: 40)
            .offset(x: offset, y: offset)
            .onFrame { frame in
                self.offset += (frame.duration * 20.0)
            }
    }
}