-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from jtrivedi/swiftui
This PR fixes #15 and allows Wave property animators to be used in SwiftUI. Previously, Animation conflicted with SwiftUI.Animation, and couldn't be resolved via Wave.Animation. It also adds a small SwiftUI + Wave demo (dragging, throwing, and animating a box).
- Loading branch information
Showing
16 changed files
with
211 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
Sample App/Wave-Sample/Wave-Sample/DragGesture+Extensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// DragGesture+Extensions.swift | ||
// Wave-Sample | ||
// | ||
// Created by Janum Trivedi on 11/5/22. | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension DragGesture.Value { | ||
|
||
internal var velocity: CGSize { | ||
let valueMirror = Mirror(reflecting: self) | ||
for valueChild in valueMirror.children { | ||
if valueChild.label == "velocity" { | ||
let velocityMirror = Mirror(reflecting: valueChild.value) | ||
for velocityChild in velocityMirror.children { | ||
if velocityChild.label == "valuePerSecond" { | ||
if let velocity = velocityChild.value as? CGSize { | ||
return velocity | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fatalError("Unable to retrieve velocity from \(Self.self)") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Sample App/Wave-Sample/Wave-Sample/SwitUIViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// SwitUIViewController.swift | ||
// Wave-Sample | ||
// | ||
// Created by Janum Trivedi on 11/2/22. | ||
// | ||
|
||
import SwiftUI | ||
|
||
import Wave | ||
|
||
struct SwiftUIView: View { | ||
|
||
let offsetAnimator = SpringAnimator<CGPoint>(spring: Spring(dampingRatio: 0.72, response: 0.7)) | ||
|
||
@State var boxOffset: CGPoint = .zero | ||
|
||
var body: some View { | ||
let size = 80.0 | ||
ZStack { | ||
RoundedRectangle(cornerRadius: size * 0.22, style: .continuous) | ||
.fill(.blue) | ||
.frame(width: size, height: size) | ||
VStack { | ||
Text("SwiftUI") | ||
.foregroundColor(.white) | ||
} | ||
|
||
}.onAppear { | ||
offsetAnimator.value = .zero | ||
|
||
// The offset animator's callback will update the `offset` state variable. | ||
offsetAnimator.valueChanged = { newValue in | ||
boxOffset = newValue | ||
} | ||
} | ||
.offset(x: boxOffset.x, y: boxOffset.y) | ||
.gesture( | ||
DragGesture() | ||
.onChanged { value in | ||
// Update the animator's target to the new drag translation. | ||
offsetAnimator.target = CGPoint(x: value.translation.width, y: value.translation.height) | ||
|
||
// Don't animate the box's position when we're dragging it. | ||
offsetAnimator.mode = .nonAnimated | ||
offsetAnimator.start() | ||
} | ||
.onEnded { value in | ||
// Animate the box to its original location (i.e. with zero translation). | ||
offsetAnimator.target = .zero | ||
|
||
// We want the box to animate to its original location, so use an `animated` mode. | ||
// This is different than the | ||
offsetAnimator.mode = .animated | ||
|
||
// Take the velocity of the gesture, and give it to the animator. | ||
// This makes the throw animation feel natural and continuous. | ||
offsetAnimator.velocity = CGPoint(x: value.velocity.width, y: value.velocity.height) | ||
offsetAnimator.start() | ||
} | ||
) | ||
} | ||
} | ||
|
||
struct SwiftUIView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
SwiftUIView() | ||
} | ||
} | ||
|
||
class SwiftUIViewController: UIViewController { | ||
|
||
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { | ||
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) | ||
|
||
title = "SwiftUI" | ||
tabBarItem.image = UIImage(systemName: "swift") | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
let hostingController = UIHostingController(rootView: SwiftUIView()) | ||
addChild(hostingController) | ||
view.addSubview(hostingController.view) | ||
hostingController.didMove(toParent: self) | ||
hostingController.view.frame = view.bounds | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.