答案 0 :(得分:0)
这就是您使用UICollectionViewFlowLayout
在代码中创建它的方式
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
或者如果您正在使用现有的集合视图
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .vertical
}
UICollectionView
具有属性BounceHorizontally
和BounceVertically
。将垂直设置为“是did
”。设置ScrollView属性PagingEnable->是
您必须添加
self.collectionView.pagingEnabled =是;
答案 1 :(得分:0)
import UIKit
class HomeViewController: UIViewController, UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource{
//Variables of Grid View
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var scrollView: UIScrollView!{
didSet{
scrollView.delegate = self
}
}
@IBOutlet weak var pageControl: UIPageControl!
var slides:[AdSlides] = [];
override func viewWillAppear(_ animated: Bool) {
setGradientBackground()
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource=self
collectionView.delegate=self
//AdSlides :start
slides = createSlides()
setupSlideScrollView(slides: slides)
pageControl.numberOfPages = slides.count
pageControl.currentPage = 0
view.bringSubviewToFront(pageControl)
//AdSlides :complete
}
//MARK: - Collection View
// Create an array that contains our data
var array = ["Air Condition", "Battery", "Engine", "Modify", "Paint","Sticker","Audio","Interior","Tyre","Wash","Tow","Check Up","Banana","Apple","Apricot"]
var photo = [UIImage(named: "ac"),UIImage(named: "battery"),UIImage(named: "engine"),UIImage(named: "modify"),UIImage(named: "paint"),UIImage(named: "sticker"),UIImage(named: "sound"),UIImage(named: "interior"),UIImage(named: "tire"),UIImage(named: "wash"),UIImage(named: "tow"),UIImage(named: "checkup"),UIImage(named: "Banana"),UIImage(named: "Apple"),UIImage(named: "Apricots")]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .vertical
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ViewCell", for: indexPath) as! CollectionViewCell
cell.gridLabel.text = array[indexPath.row]
cell.gridImage.image = photo[indexPath.row]
return cell
}
//MARK: - End Of Collection View
//MARK: - Function For Blue Gradient Background
func setGradientBackground() {
let colorTop = UIColor(red: 0.0/255.0, green: 68.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 0.0/255.0, green: 206.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorTop, colorBottom]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.frame = self.view.bounds
self.view.layer.insertSublayer(gradientLayer, at:0)
}
// MARK: - ADSLIDE's
func createSlides() -> [AdSlides] {
//API IMAGES
let dataImg = NSData(contentsOf: URL(string: "https://source.unsplash.com/random")!)
let dataImg2 = NSData(contentsOf: URL(string: "https://source.unsplash.com/random")!)
let dataImg3 = NSData(contentsOf: URL(string: "https://source.unsplash.com/random")!)
let slide1:AdSlides = Bundle.main.loadNibNamed("AdSlides", owner: self, options: nil)?.first as! AdSlides
slide1.imageView.image = UIImage(named: "image1")
let slide2:AdSlides = Bundle.main.loadNibNamed("AdSlides", owner: self, options: nil)?.first as! AdSlides
slide2.imageView.image = UIImage(named: "image2")
let slide3:AdSlides = Bundle.main.loadNibNamed("AdSlides", owner: self, options: nil)?.first as! AdSlides
slide3.imageView.image = UIImage(named: "image3")
let slide4:AdSlides = Bundle.main.loadNibNamed("AdSlides", owner: self, options: nil)?.first as! AdSlides
slide4.imageView.image = UIImage(named: "Apple")
let slide5:AdSlides = Bundle.main.loadNibNamed("AdSlides", owner: self, options: nil)?.first as! AdSlides
slide5.imageView.image = UIImage(named: "Banana")
let slide6:AdSlides = Bundle.main.loadNibNamed("AdSlides", owner: self, options: nil)?.first as! AdSlides
slide6.imageView.image = UIImage(data: dataImg3! as Data)
let slide7:AdSlides = Bundle.main.loadNibNamed("AdSlides", owner: self, options: nil)?.first as! AdSlides
slide7.imageView.image = UIImage(data: dataImg2! as Data)
let slide8:AdSlides = Bundle.main.loadNibNamed("AdSlides", owner: self, options: nil)?.first as! AdSlides
slide8.imageView.image = UIImage(data: dataImg! as Data)
return [slide1, slide2, slide3, slide4, slide5,slide6,slide7,slide8]
}
func setupSlideScrollView(slides : [AdSlides]) {
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: view.frame.height)
scrollView.isPagingEnabled = true
for i in 0 ..< slides.count {
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
scrollView.addSubview(slides[i])
}
}
/*
* default function called when view is scolled. In order to enable callback
* when scrollview is scrolled, the below code needs to be called:
* slideScrollView.delegate = self or
*/
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
/*
* below code changes the background color of view on paging the scrollview
*/
// self.scrollView(scrollView, didScrollToPercentageOffset: percentageHorizontalOffset)
/*
* below code scales the imageview on paging the scrollview
*/
//cancel horizontal scrollView
if scrollView.contentOffset.y > 0 || scrollView.contentOffset.y < 0 {
scrollView.contentOffset.y = 0
}
}
}