Skip to content

Commit

Permalink
fixed motion effects not rotating on first use + shader library compi…
Browse files Browse the repository at this point in the history
…lation fix
  • Loading branch information
bpisano committed Nov 13, 2024
1 parent c4d1c5a commit fed378b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ Image(.stickerIcon)
.stickerMotionEffect(.pointerHover(intensity: 0.5))
```

Using the `.animation` view modifier allows you to control the animation of the effect.

```swift
Image(.stickerIcon)
.animation(.snappy) { view in
view
.stickerEffect()
.stickerMotionEffect(.pointerHover(intensity: 0.5))
}
```

The following motion effects are available:

| Effect | Description |
Expand Down Expand Up @@ -97,3 +108,24 @@ extension StickerMotionEffect where Self == MyMotionEffect {
static var myMotionEffect: Self { .init() }
}
```

## Compiling Shaders

On iOS 18, macOS 15 and visionOS 2, Apple added the ability to compile Metal shaders at runtime to prevent frame delay on first use. You can compile the Sticker shaders as soon as your app launches by calling the following function:

```swift
import SwiftUI
import Sticker

@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.task {
await ShaderLibrary.compileStickerShaders()
}
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public struct DragStickerMotionEffect: StickerMotionEffect {
let yRotation: Double = motionObserver.motion.transform.y / size.height
view
.rotation3DEffect(
motionObserver.motion.isActive ?.radians((xRotation) * intensity) : .degrees(0),
.radians((xRotation) * intensity),
axis: (0, 1, 0)
)
.rotation3DEffect(
motionObserver.motion.isActive ? .radians((yRotation) * intensity) : .degrees(0),
.radians((yRotation) * intensity),
axis: (-1, 0, 0)
)
.gesture(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public struct PointerHoverStickerMotionEffect: StickerMotionEffect {
let yRotation: Double = motionObserver.motion.transform.y / size.height
view
.rotation3DEffect(
motionObserver.motion.isActive ?.radians((xRotation) * intensity) : .degrees(0),
.radians((xRotation) * intensity),
axis: (0, 1, 0)
)
.rotation3DEffect(
motionObserver.motion.isActive ? .radians((yRotation) * intensity) : .degrees(0),
.radians((yRotation) * intensity),
axis: (-1, 0, 0)
)
.onContinuousHover { phase in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class StickerMotionObserver {
self.onChange = onChange
}

@MainActor
func update(motion: StickerMotion) {
self.motion = motion
onChange(motion)
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sticker/Utils/Extensions/ShaderLibraryExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ extension ShaderLibrary {
.float(reflectionIntensity)
)
}
}

public extension ShaderLibrary {
@available(iOS 18.0, macOS 15.0, visionOS 2.0, tvOS 18.0, watchOS 11.0, *)
static func compileStickerShaders() async throws {
try await foilShader().compile(as: .colorEffect)
Expand Down

0 comments on commit fed378b

Please sign in to comment.