Created
June 24, 2019 15:10
-
-
Save hsiaoer/74999e09e4caee705d73a4dce51746b7 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
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