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
struct Page { | |
let animationName: String | |
let title: String | |
let description: String | |
} |
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, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { | |
// MARK:- outlets for the viewController | |
@IBOutlet weak var collectionView: UICollectionView! | |
@IBOutlet weak var pageControl: UIPageControl! | |
@IBOutlet weak var getStartedButton: UIButton! | |
// data for the Onboarding Screens | |
let pages: [Page] = [Page(animationName: "animation1", title: "Learn to Code", description: "Find awesome tutorials on how to code and improve your coding practices"), | |
Page(animationName: "animation2", title: "Code with Friends", description: "Practice with friends and solve problems together to earn points"), |
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
// MARK:- lifeCycle methods for the ViewController | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// to make the button rounded | |
self.getStartedButton.layer.cornerRadius = 20 | |
// register the custom CollectionViewCell and assign the delegates to the ViewController | |
self.collectionView.backgroundColor = .white | |
self.collectionView.dataSource = self |
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
// MARK:- outlet functions for the viewController | |
@IBAction func pageChanged(_ sender: Any) { | |
let pc = sender as! UIPageControl | |
// scrolling the collectionView to the selected page | |
collectionView.scrollToItem(at: IndexPath(item: pc.currentPage, section: 0), | |
at: .centeredHorizontally, animated: true) | |
} | |
@IBAction func getStartedButtonTapped(_ sender: Any) { |
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
// MARK:- collectionView dataSource & collectionView FlowLayout delegates | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return pages.count | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OnboardingCollectionViewCell.identifier, | |
for: indexPath) as! OnboardingCollectionViewCell | |
// function for configuring the cell, defined in the Custom cell class | |
cell.configureCell(page: pages[indexPath.item]) |
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
// Custom UICollectionViewCell | |
class OnboardingCollectionViewCell: UICollectionViewCell { | |
@IBOutlet weak var animationContainer: UIView! | |
@IBOutlet weak var titleLabel: UILabel! | |
@IBOutlet weak var descriptionTextView: UITextView! | |
static let identifier = "OnboardingCollectionViewCell" | |
// Instance of the Lottie AnimationView |
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
// function to configure the cell | |
func configureCell(page: Page){ | |
// define the animation and the size | |
animation = AnimationView(name: page.animationName) | |
animation.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height * 0.8) | |
// customize the animation | |
animation.animationSpeed = 1 | |
animation.loopMode = .loop |
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
extension UIView { | |
@discardableResult | |
func fromNib<T : UIView>() -> T? { | |
guard let contentView = Bundle(for: type(of: self)).loadNibNamed(String(describing: type(of: self)), | |
owner: self, options: nil)?.first as? T else { | |
return nil | |
} | |
self.addSubview(contentView) | |
contentView.translatesAutoresizingMaskIntoConstraints = false | |
contentView.layoutAttachAll() |
OlderNewer