Skip to content

Instantly share code, notes, and snippets.

@emin-grbo
Last active August 5, 2021 21:28
Show Gist options
  • Save emin-grbo/24b138cff5c4f2da88b927df752c9ab3 to your computer and use it in GitHub Desktop.
Save emin-grbo/24b138cff5c4f2da88b927df752c9ab3 to your computer and use it in GitHub Desktop.
CardViewWithShimmer
import SwiftUI
import CoreMotion
struct CardView: View {
@StateObject var motion = MotionManager()
var body: some View {
ZStack {
Color.white
Rectangle()
.fill(LinearGradient(gradient: Gradient(colors: [Color.black, Color.white, Color.black]),
startPoint: UnitPoint(x: motion.fy*2, y: motion.fy*2), // <- MoneyShot! 🤘
endPoint: UnitPoint(x: motion.fx*8, y: motion.fx*8)))
.cornerRadius(20)
.frame(height: 250)
.padding()
Image(systemName: "applelogo")
.font(Font.system(size: 32, weight: .bold, design: .rounded))
}
.shadow(color: .gray, radius: 20, x: 0, y: 30)
}
}
import SwiftUI
import CoreMotion
class MotionManager: ObservableObject {
private let motionManager = CMMotionManager()
var fx: CGFloat = 0
var fy: CGFloat = 0
var fz: CGFloat = 0
var dx: Double = 0
var dy: Double = 0
var dz: Double = 0
init() {
motionManager.startDeviceMotionUpdates(to: .main) { data, error in
guard let newData = data?.gravity else { return }
self.dx = newData.x
self.dy = newData.y
self.dz = newData.z
self.fx = CGFloat((newData.x))
self.fy = CGFloat((newData.y))
self.fz = CGFloat((newData.z))
print("X: \(self.fx), Y: \(self.fy)")
self.objectWillChange.send()
}
}
// Used to stop motion manager if needed.
func shutdown() {
motionManager.stopDeviceMotionUpdates()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment