Skip to content

Instantly share code, notes, and snippets.

@hsiaoer
Created June 24, 2019 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsiaoer/74999e09e4caee705d73a4dce51746b7 to your computer and use it in GitHub Desktop.
Save hsiaoer/74999e09e4caee705d73a4dce51746b7 to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
let background = UIImage(named: "clouds.png")
var animationDuration = 12.0
override func viewDidLoad() {
///...
backgroundView = initialBackground()
backgroundViewDelayed = initialBackground()
view.addSubview(backgroundView)
view.addSubview(backgroundViewDelayed)
/// First background view starts immediately
startAnimation(on: backgroundView, delay: 0.0)
/// Second view starts after a delay
startAnimation(on: backgroundViewDelayed, delay: animationDuration / 2)
}
func initialBackground() -> UIImageView {
let shiftedLeft = CGRect(origin: CGPoint(x: -view.bounds.width, y: view.bounds.minY),
size: view.bounds.size)
let view = UIImageView(frame: shiftedLeft)
view.contentMode = .scaleAspectFill
view.image = background
return view
}
/// Create sliding animation that repeats on a delay
func startAnimation(on imageView: UIImageView, delay: Double) {
UIView.animate(withDuration: animationDuration, delay: delay, options: [.repeat, .curveLinear], animations: {
imageView.frame = CGRect(
origin: CGPoint(x: self.view.bounds.width, y: self.view.bounds.minY),
size: self.view.bounds.size
)
})
}
///...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment