Skip to content

Instantly share code, notes, and snippets.

@gitcrtn
Created September 18, 2024 05:32
Show Gist options
  • Select an option

  • Save gitcrtn/fb99bd7f42b65a76b77a4191d81bcbcd to your computer and use it in GitHub Desktop.

Select an option

Save gitcrtn/fb99bd7f42b65a76b77a4191d81bcbcd to your computer and use it in GitHub Desktop.
digital crown test by apple watch (2022/1/9)
import ClockKit
class ComplicationController: NSObject, CLKComplicationDataSource {
// MARK: - Complication Configuration
func getComplicationDescriptors(handler: @escaping ([CLKComplicationDescriptor]) -> Void) {
let descriptors = [
CLKComplicationDescriptor(identifier: "complication", displayName: "WatchBlock", supportedFamilies: CLKComplicationFamily.allCases)
// Multiple complication support can be added here with more descriptors
]
// Call the handler with the currently supported complication descriptors
handler(descriptors)
}
func handleSharedComplicationDescriptors(_ complicationDescriptors: [CLKComplicationDescriptor]) {
// Do any necessary work to support these newly shared complication descriptors
}
// MARK: - Timeline Configuration
func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
// Call the handler with the last entry date you can currently provide or nil if you can't support future timelines
handler(nil)
}
func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
// Call the handler with your desired behavior when the device is locked
handler(.showOnLockScreen)
}
// MARK: - Timeline Population
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
// Call the handler with the current timeline entry
handler(nil)
}
func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
// Call the handler with the timeline entries after the given date
handler(nil)
}
// MARK: - Sample Templates
func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
// This method will be called once per supported complication, and the results will be cached
handler(nil)
}
}
import SwiftUI
import SpriteKit
struct ContentView: View {
@State var scrollAmount = 0.0
private var scene = GameScene()
var body: some View {
ZStack {
SpriteView(scene: scene)
Text("Scroll: \(scrollAmount)")
.focusable(true)
.digitalCrownRotation($scrollAmount)
.onChange(of: scrollAmount) {newValue in
self.scene.padAction(newValue)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SpriteKit
class GameScene: SKScene {
private var square: SKSpriteNode?
private var lastAmount = 0.0
private let velocity = 0.005
override func sceneDidLoad() {
self.square = SKSpriteNode(
color: UIColor.green,
size: CGSize(width: self.frame.width / 3, height: self.frame.height / 10)
)
if let square = self.square {
square.position = CGPoint(
x: self.frame.midX,
y: self.frame.height / 5
)
self.addChild(square)
}
}
private func clampWidth(_ width: CGFloat, _ delta: CGFloat) -> CGFloat {
return max(min(width, self.frame.width - delta), delta)
}
func padAction(_ amount: Double) {
if let square = self.square {
square.position.x = clampWidth(self.frame.midX * CGFloat(amount - lastAmount) * velocity + square.position.x, square.size.width / 2)
}
lastAmount = amount
}
}
import SwiftUI
@main
struct WatchBlockApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
ContentView()
}
}
}
}
@gitcrtn
Copy link
Author

gitcrtn commented Sep 18, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment