1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| @IBOutlet weak var collectionView: UICollectionView! @IBOutlet weak var pageControl: UIPageControl! var colletcionData:[String] = ["yindao1","yindao2","yindao3","yindao4"]; override func viewDidLoad() { super.viewDidLoad()
self.initCollectionView(); self.initPageControl(); } func initCollectionView(){ self.collectionView.register(UINib.init(nibName: "WelcomeCell", bundle: nil), forCellWithReuseIdentifier: "WelcomeCell"); self.collectionView.showsHorizontalScrollIndicator = false; self.collectionView.showsVerticalScrollIndicator = false; self.collectionView.backgroundColor = UIColor.clear; self.collectionView.isScrollEnabled = true; let flowLayout = UICollectionViewFlowLayout(); flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal; flowLayout.minimumInteritemSpacing = 0; flowLayout.minimumLineSpacing = 0; self.collectionView.collectionViewLayout = flowLayout; self.collectionView.isPagingEnabled = true; self.collectionView.dataSource = self; self.collectionView.delegate = self; } func initPageControl(){ self.pageControl.numberOfPages = self.colletcionData.count; self.pageControl.currentPage = 0; self.pageControl.setValue(UIImage.init(named: "pageControl1"), forKey: "_pageImage") self.pageControl.setValue(UIImage.init(named: "pageControl2"), forKey: "_currentPageImage") }
func numberOfSections(in collectionView: UICollectionView) -> Int { return 1; } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return colletcionData.count; } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let itemdata = colletcionData[indexPath.row]; let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WelcomeCell", for: indexPath) as! WelcomeCell; cell.inImageView.image = UIImage.init(named: itemdata) return cell; } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: self.collectionView.frame.width, height: self.collectionView.frame.height); } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if(indexPath.row == (self.colletcionData.count-1)){ ZJUserDefaults.setValue("false", forKey: "isFirstLoad"); let loginViewController = LoginViewController(); AppDelegate.appDelegate?.window?.rootViewController = loginViewController; } } func scrollViewDidScroll(_ scrollView: UIScrollView) { let pageWidth = self.collectionView.frame.width; let currPage = Int(floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1); self.pageControl.currentPage = currPage; if(currPage == self.colletcionData.count - 1){ self.pageControl.isHidden = true; }else{ self.pageControl.isHidden = false; } }
|