Skip to content

Instantly share code, notes, and snippets.

@aravindhkumar23
Last active November 29, 2016 08:58
Show Gist options
  • Save aravindhkumar23/2a28dacb68e44032677c3cad4928b842 to your computer and use it in GitHub Desktop.
Save aravindhkumar23/2a28dacb68e44032677c3cad4928b842 to your computer and use it in GitHub Desktop.
carousel view programatically swift includes [collection view, pageControl, with 2 button and 3 labels] swift 2.3
// Swift 2.3 Xcode 7.3.1
import UIKit
class swipeViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
let fontName: String = "Aileron-Regular"
let fontSize: CGFloat = 16
var collectionView: UICollectionView!
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
var pageControl = UIPageControl()
var interSpacing : CGFloat?
var buttonY = CGFloat(0)
var pageContent : [[String:String]] = [[String:String]]()
var title2Textlabel : UILabel!
var quoteTextlabel : UILabel!
var callButton : UIButton!
var acceptButton : UIButton!
var infoLabel : UILabel!
let buttonHeight: CGFloat = 44
var buttonWidth = CGFloat(130)
var buttonFrameSet = false
var buttonTitle = ""
override func viewDidLoad() {
super.viewDidLoad()
buttonTitle = "Report"
pageContent = [
["title":"Tell us","quote":"My quote 1.","image":"image1.png","info":"Call 123 "],
["title":"Stay Anonymous","quote":"My quote 2.","image":"image2.png","info":"Call 123 "],
["title":"Safe & Secure","quote":"My quote 3.","image":"image3.png","info":"Call 123 "]
]
self.view.addSubview(addCollectionView())
pageControl = UIPageControl(frame: CGRectMake(0, 250, self.view.frame.width , 20))
pageControl.backgroundColor = UIColor.clearColor()
pageControl.addTarget(self, action: #selector(OnBoardingViewController.changePage(_:)), forControlEvents: UIControlEvents.ValueChanged)
self.view.addSubview(pageControl)
self.pageControl.currentPageIndicatorTintColor = UIColor.grayColor()
self.pageControl.numberOfPages = pageContent.count
self.pageControl.currentPage = 0
self.pageControl.pageIndicatorTintColor = UIColor.whiteColor().colorWithAlphaComponent(0.4)
self.pageControl.currentPageIndicatorTintColor = UIColor.whiteColor()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
}
func addCollectionView() -> UIView{
parentViewHeight = self.view.frame.height
buttonWidth = self.view.frame.width - 50
layout.scrollDirection = .Horizontal
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
collectionView = UICollectionView(frame: CGRectMake(0, -10, self.view.frame.width , self.view.frame.height + 10),
collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.clearColor()
collectionView.showsHorizontalScrollIndicator = false
collectionView.bounces = false
collectionView.clipsToBounds = true
collectionView.pagingEnabled = true
collectionView.collectionViewLayout = layout
interSpacing = 10
return collectionView
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.height)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pageContent.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let content = pageContent[indexPath.row] as NSDictionary
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
let imageView = UIImageView(frame: CGRect(x: 0, y: 10, width: collectionView.frame.width, height: collectionView.frame.height))
imageView.image = UIImage(named: content.valueForKey("image") as? String ?? "")
imageView.backgroundColor = UIColor.clearColor()
let title2TextlabelY = 100.0
title2Textlabel = UILabel(frame: CGRect(x: 0, y: title2TextlabelY, width: self.view.frame.width, height:50))
title2Textlabel.text = "Dummy Text"// overwrite with real data from Dict
title2Textlabel.textColor = UIColor.whiteColor()
title2Textlabel.backgroundColor = UIColor.clearColor()
title2Textlabel.textAlignment = .Center
title2Textlabel.numberOfLines = 0
title2Textlabel.font = UIFont(name: MYFONTS.LIGHT , size: 40.0)
let quoteTextlabelY = title2TextlabelY + title2Textlabel.frame.height + 20
quoteTextlabel = UILabel(frame: CGRect(x: 0, y: quoteTextlabelY, width: self.view.frame.width, height:70))
quoteTextlabel.text = "Quotes" // overwrite with real data from Dict
quoteTextlabel.textColor = UIColor.whiteColor()
quoteTextlabel.backgroundColor = UIColor.clearColor()
quoteTextlabel.textAlignment = .Center
quoteTextlabel.numberOfLines = 0
quoteTextlabel.font = UIFont(name: MYFONTS.REGULAR , size: 20.0)
buttonY = quoteTextlabelY + quoteTextlabel.frame.height + 10
// create button one time
if(buttonFrameSet == false){
createButton()
}
cell.contentView.addSubview(imageView)
cell.contentView.addSubview(quoteTextlabel)
cell.contentView.addSubview(title2Textlabel)
title2Textlabel.text = content.valueForKey("title") as? String ?? ""
quoteTextlabel.text = content.valueForKey("quote") as? String ?? ""
infoLabel.text = content.valueForKey("info") as? String ?? ""
cell.backgroundColor = UIColor.clearColor()
imageView.frame.origin.x = (cell.frame.width / 2) - (imageView.frame.width / 2)
return cell
}
func createButton(){
// acceptButton view
acceptButton = UIButton(type: UIButtonType.System) as UIButton! //this is Swift2.0 in this place use your code
acceptButton.frame = CGRectMake(self.view.frame.width/2 + 5, buttonY, buttonWidth , buttonHeight)
acceptButton.tintColor = UIColor(red:0.02, green:0.26, blue:0.89, alpha:1.0) //MYCOLOR.MAIN_TITLE_DARK
acceptButton.backgroundColor = UIColor.whiteColor()
acceptButton.layer.cornerRadius = 3
acceptButton.setTitle(buttonTitle, forState: UIControlState.Normal)
acceptButton.titleLabel!.font = UIFont(name: MYFONTS.REGULAR, size: 20.0)
acceptButton.center.x = self.view.center.x
acceptButton.addTarget(self, action: #selector(reportAction), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(acceptButton)
let callButtonY = buttonY + acceptButton.frame.height + 10
callButton = UIButton(type: UIButtonType.System) as UIButton! //this is Swift2.0 in this place use your code
callButton.frame = CGRectMake(5, callButtonY, buttonWidth , buttonHeight)
callButton.tintColor = UIColor.whiteColor()
callButton.layer.borderColor = UIColor.whiteColor().CGColor
callButton.layer.borderWidth = 1
callButton.backgroundColor = UIColor.clearColor()
callButton.layer.cornerRadius = 3
callButton.setTitle("Call 123", forState: UIControlState.Normal)
callButton.titleLabel!.font = UIFont(name: MYFONTS.REGULAR, size: 20.0)
callButton.center.x = self.view.center.x
callButton.addTarget(self, action: #selector(callButtonAction), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(callButton)
pageControl.frame.origin.y = callButton.frame.origin.y + callButton.frame.height + 10
let infoLabelY = pageControl.frame.origin.y + pageControl.frame.height + infoTopSpacing
infoLabel = UILabel(frame: CGRect(x: 0, y: infoLabelY, width: self.view.frame.width, height:50))
infoLabel.text = ""
infoLabel.textColor = UIColor.whiteColor()
infoLabel.backgroundColor = UIColor.clearColor()
infoLabel.textAlignment = .Center
infoLabel.numberOfLines = 0
infoLabel.font = UIFont(name: MYFONTS.LIGHT , size: 19.0)
self.view.addSubview(infoLabel)
buttonFrameSet = true
}
// MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
func changePage(sender: AnyObject) -> () {
let x = CGFloat(pageControl.currentPage) * collectionView.frame.size.width
collectionView.setContentOffset(CGPointMake(x, 0), animated: true)
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}
//OK Button Action
func reportAction() {
print("report here clicked")
}
//OK Button Action
func callButtonAction() {
print("call clicked")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment