• Stars
    star
    106
  • Rank 315,627 (Top 7 %)
  • Language
    Swift
  • License
    BSD 2-Clause "Sim...
  • Created over 4 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Per-frame actions for SwiftUI

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)
            }
    }
}