Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianselig/c14e7801b21c0215329060f2734e81fb to your computer and use it in GitHub Desktop.
Save christianselig/c14e7801b21c0215329060f2734e81fb to your computer and use it in GitHub Desktop.
import UIKit
import SwiftUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let box = UIView()
box.frame = CGRect(x: (view.bounds.width / 2.0) - 100.0, y: 0.0, width: 100.0, height: 100.0)
box.backgroundColor = .systemGreen
view.addSubview(box)
// An ever so slight delay is required to sync them up
UIView.animate(withDuration: 0.5, delay: 0.025, options: [.curveLinear, .repeat, .autoreverse]) {
box.frame.origin.y = 600.0
}
let hostingController = UIHostingController(rootView: ContentView())
view.addSubview(hostingController.view)
hostingController.didMove(toParent: self)
hostingController.view.frame = CGRect(x: view.bounds.width / 2.0, y: 0.0, width: view.bounds.width / 2.0, height: view.bounds.height)
}
}
struct ContentView: View {
@State var offsetY = 0.0
var body: some View {
ZStack(alignment: .topLeading) {
Color.clear
Rectangle()
.foregroundColor(.red)
.frame(width: 100.0, height: 100.0)
.offset(y: offsetY)
.onAppear {
let animation = Animation.linear(duration: 0.5).repeatForever()
withAnimation(animation) {
offsetY = 600.0
}
}
}
.ignoresSafeArea()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment