Skip to content

Instantly share code, notes, and snippets.

@ayoub-9
Created February 26, 2019 17:28
Show Gist options
  • Save ayoub-9/718ca6f99406ef75c56dce36350fdc99 to your computer and use it in GitHub Desktop.
Save ayoub-9/718ca6f99406ef75c56dce36350fdc99 to your computer and use it in GitHub Desktop.
//
// Explore.swift
// IINII
//
// Created by Ayoub Alrshidi on 21/06/1440 AH.
// Copyright © 1440 Ayoub Alrshidi. All rights reserved.
//
import UIKit
class Explore: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
//MARK: - IDs
let ImageCellID = "imageCell"
let albumId = "CellId"
//MARK: - ARRY IMAGES
let imagesArray = ["image1", "image2", "image3", "image4", "image5"]
let albumsArray = ["album1", "album2", "album3", "album4", "album5", "album6", "album7", "album8", "album9"]
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 10
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .clear
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
SetupViews()
}
func SetupViews(){
view.backgroundColor = .lightGray
//register cell class
collectionView.register(ImagesCell.self, forCellWithReuseIdentifier: ImageCellID)
collectionView.register(AlbumaCell.self, forCellWithReuseIdentifier: albumId)
//MARK: - Add CollectonView List to View
collectionView.delegate = self
collectionView.dataSource = self
view.addSubview(collectionView)
collectionView.setAnchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 1 {
return 20
}
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: albumId, for: indexPath) as! AlbumaCell
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageCellID, for: indexPath) as! ImagesCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.section == 1 {
let itemSize = UIScreen.main.bounds.width/3 - 3
return CGSize(width: itemSize, height: 230)
}
//next Section
return CGSize(width: 400, height: 200)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if section == 1 {
return UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
}
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
class ImagesCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var Images: [String]? {
didSet{
collectionView.reloadData()
}
}
//MARK: - IDs
let cellId = "cell"
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 30
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .clear
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
//MARK: - regsiter cell class
collectionView.register(iconsCell.self, forCellWithReuseIdentifier: cellId)
}
func setup(){
backgroundColor = .red
//MARK: - CollectionView This From Cell file
addSubview(collectionView)
collectionView.setAnchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.showsHorizontalScrollIndicator = false
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! iconsCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height: frame.height - 20 )
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private class iconsCell: UICollectionViewCell {
//MARK: Properties
let ImageView: UIImageView = {
let iv = UIImageView()
iv.backgroundColor = .lightGray
iv.contentMode = .scaleToFill
iv.clipsToBounds = true
iv.layer.cornerRadius = 15
return iv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup(){
addSubview(ImageView)
ImageView.setAnchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
}
class AlbumaCell: UICollectionViewCell {
var Images2: [String]? {
didSet{
}
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .green
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@ayoub-9
Copy link
Author

ayoub-9 commented Feb 26, 2019

//
// Extensions.swift
// Sections
//
// Created by Pavel Bogart on 2/10/17.
// Copyright © 2017 Pavel Bogart. All rights reserved.
//

import UIKit

extension UIView {

func setCellShadow() {
    self.layer.shadowColor = UIColor.darkGray.cgColor
    self.layer.shadowOffset = CGSize(width: 0, height: 1)
    self.layer.shadowOpacity = 1
    self.layer.shadowRadius = 1.0
    self.layer.masksToBounds = false
    self.clipsToBounds = false
    self.layer.cornerRadius = 15
}

func setAnchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?,
            bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?,
            paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat,
            paddingRight: CGFloat, width: CGFloat = 0, height: CGFloat = 0) {
    
    self.translatesAutoresizingMaskIntoConstraints = false
    
    if let top = top {
        self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
    }
    
    if let left = left {
        self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
    }
    
    if let bottom = bottom {
        self.bottomAnchor.constraint(equalTo: bottom, constant: paddingBottom).isActive = true
    }
    
    if let right = right {
        self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
    }
    
    if width != 0 {
        self.widthAnchor.constraint(equalToConstant: width).isActive = true
    }
    
    if height != 0 {
        self.heightAnchor.constraint(equalToConstant: height).isActive = true
    }
}

var safeTopAnchor: NSLayoutYAxisAnchor {
    if #available(iOS 11.0, *) {
        return safeAreaLayoutGuide.topAnchor
    }
    return topAnchor
}

var safeLeftAnchor: NSLayoutXAxisAnchor {
    if #available(iOS 11.0, *) {
        return safeAreaLayoutGuide.leftAnchor
    }
    return leftAnchor
}

var safeBottomAnchor: NSLayoutYAxisAnchor {
    if #available(iOS 11.0, *) {
        return safeAreaLayoutGuide.bottomAnchor
    }
    return bottomAnchor
}

var safeRightAnchor: NSLayoutXAxisAnchor {
    if #available(iOS 11.0, *) {
        return safeAreaLayoutGuide.rightAnchor
    }
    return rightAnchor
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment