Skip to content

Instantly share code, notes, and snippets.

View Shubham0812's full-sized avatar
🎯
Focusing

Shubham Kr. Singh Shubham0812

🎯
Focusing
View GitHub Profile
struct Page {
let animationName: String
let title: String
let description: String
}
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"),
// 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
// 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) {
// 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])
// 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
// 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
extension UIButton {
// iconName - SFSymbol Name
// size - Size of the Symbol in points
// scale - .small, .medium, .large
// weight - .ultralight, .thin, .light, .regular, .medium, .semibold, .bold, .heavy, .black
// tintColor - Color of the Symbol
// backgroundColor - Background color of the button
func setSFSymbol(iconName: String, size: CGFloat, weight: UIImage.SymbolWeight,
scale: UIImage.SymbolScale, tintColor: UIColor, backgroundColor: UIColor) {
let symbolConfiguration = UIImage.SymbolConfiguration(pointSize: size, weight: weight, scale: scale)
extension UIButton {
// padding - the spacing between the Image and the Title
func centerTitleVertically(padding: CGFloat = 12.0) {
guard let imageViewSize = self.imageView?.frame.size, let titleLabelSize = self.titleLabel?.frame.size
else {
return
}
let totalHeight = imageViewSize.height + titleLabelSize.height + padding
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()