Created
September 26, 2022 01:21
-
-
Save christianselig/c14e7801b21c0215329060f2734e81fb to your computer and use it in GitHub Desktop.
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
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