Skip to content

Instantly share code, notes, and snippets.

@ajaybeniwal
Created July 24, 2018 14:51
Show Gist options
  • Save ajaybeniwal/8e32813df2a4e5033b7fb67f0cde06d2 to your computer and use it in GitHub Desktop.
Save ajaybeniwal/8e32813df2a4e5033b7fb67f0cde06d2 to your computer and use it in GitHub Desktop.
//
// FeatureViewController.swift
// IOS11AppStoreClone
//
// Created by Ajay Singh on 24/7/18.
// Copyright © 2018 Ajay Singh. All rights reserved.
//
import UIKit
class FeatureViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
var collectionView:UICollectionView = {let collectionView = UICollectionView(frame:CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
return collectionView;
}()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Features"
self.configureCollectionView();
}
func configureCollectionView() ->Void{
self.view.addSubview(collectionView)
collectionView.register(FeatureViewCell.self, forCellWithReuseIdentifier: "featureCell")
collectionView.register(ImageViewCardCell.self, forCellWithReuseIdentifier: "imageViewCard")
// collectionView.backgroundColor = UIColor(red: 223/255, green: 226/255, blue: 229/255, alpha: 1)
collectionView.backgroundColor = UIColor.white
// let layoutGuide = self.view.safeAreaLayoutGuide
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true
collectionView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
collectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0).isActive = true
collectionView.dataSource = self
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout;
flowLayout.minimumLineSpacing = 0
flowLayout.minimumInteritemSpacing = 0
flowLayout.sectionInset = UIEdgeInsets(top: 0, left:0, bottom: 0, right:0)
collectionView.delegate = self
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(indexPath.row%2==0){
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageViewCard", for: indexPath) as! ImageViewCardCell
return cell;
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "featureCell", for: indexPath) as! FeatureViewCell
return cell;
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if(indexPath.row%2==0){
return CGSize(width: self.view.frame.size.width-40, height: 300)
}
return CGSize(width: self.view.frame.size.width, height: 200)
}
}
class FeatureViewCell:UICollectionViewCell,UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
let collectionView:UICollectionView = {
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.backgroundColor = UIColor.white;
return collectionView
}()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setupSubViews()
}
func setupSubViews() ->Void{
self.backgroundColor = UIColor.black;
self.contentView.addSubview(collectionView);
collectionView.snp.makeConstraints { (make) in
make.leading.trailing.bottom.top.equalTo(self.contentView)
}
collectionView.delegate = self;
collectionView.dataSource = self;
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout;
flowLayout.scrollDirection = .horizontal;
collectionView.showsHorizontalScrollIndicator = false;
collectionView.register(AppCell.self, forCellWithReuseIdentifier: "appCell")
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "appCell", for: indexPath) as! AppCell
return cell;
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height: 200)
}
}
class AppCell:UICollectionViewCell{
let cardView:UIView = {
let cardView = UIView()
cardView.backgroundColor = UIColor.white
cardView.layer.cornerRadius = 10
cardView.layer.masksToBounds = true
cardView.isUserInteractionEnabled = true
return cardView
}()
let imageView:UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "CardImage5")
return imageView
}()
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setupSubViews();
}
func setupSubViews() ->Void{
self.contentView.addSubview(cardView)
cardView.snp.makeConstraints { (make) in
make.leading.equalTo(self.contentView).offset(20)
make.trailing.equalTo(self.contentView).offset(-20)
make.top.equalTo(self.contentView).offset(20)
make.bottom.equalTo(self.contentView).offset(-20)
}
cardView.addSubview(imageView)
imageView.snp.makeConstraints { (make) in
make.leading.trailing.top.bottom.equalTo(cardView)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment