第七天:SwiftLoginGuide
参考链接:https://www.jianshu.com/p/a7834f90caf2
1 import UIKit 2 3 class MainCustomNavigationController: UINavigationController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 self.view.backgroundColor = UIColor.cyan 9 10 if isFirstLaunch() { 11 perform(#selector(showGuideViewController), with: nil, afterDelay: 0.01) 12 } else if isLogined() { 13 // perform(#selector(showLoginViewController), with: nil, afterDelay: 0.01) 14 } else { 15 perform(#selector(showLoginViewController), with: nil, afterDelay: 0.01) 16 } 17 18 // Do any additional setup after loading the view. 19 } 20 21 func isFirstLaunch() -> Bool { 22 return true 23 } 24 25 func isLogined() -> Bool { 26 return false 27 } 28 29 @objc func showLoginViewController() { 30 let storyboard = UIStoryboard (name: "LoginViewController", bundle: nil) 31 let loginVC = storyboard.instantiateViewController(withIdentifier: "loginVCIdentifier") as! LoginViewController 32 33 present(loginVC, animated: false) { 34 // 35 } 36 } 37 38 func isFinshGuideViewController() { 39 dismiss(animated: false) { 40 // 41 } 42 43 showLoginViewController() 44 } 45 46 @objc func showGuideViewController() { 47 let storyboard = UIStoryboard (name: "GuideViewController", bundle: nil) 48 let guideVC = storyboard.instantiateViewController(withIdentifier: "guideVCIdentifier") as! GuideViewController 49 50 present(guideVC, animated: false) { 51 // 52 } 53 } 54 55 override func didReceiveMemoryWarning() { 56 super.didReceiveMemoryWarning() 57 // Dispose of any resources that can be recreated. 58 } 59 60 61 /* 62 // MARK: - Navigation 63 64 // In a storyboard-based application, you will often want to do a little preparation before navigation 65 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 66 // Get the new view controller using segue.destinationViewController. 67 // Pass the selected object to the new view controller. 68 } 69 */ 70 71 }
1 import UIKit 2 3 class GuideViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate { 4 5 @IBOutlet weak var collectionView: UICollectionView! 6 @IBOutlet weak var skipBtn: UIButton! 7 @IBOutlet weak var continueBtn: UIButton! 8 @IBOutlet weak var pageControler: UIPageControl! 9 10 @IBOutlet weak var shipBtnTop: NSLayoutConstraint! 11 @IBOutlet weak var continueBtnTop: NSLayoutConstraint! 12 @IBOutlet weak var pageControlerBottom: NSLayoutConstraint! 13 14 lazy var pages: [PageItemModel] = [ 15 PageItemModel (dict: ["imageName":"moreInfo", "title":"发现更多", "detail":"在这里发现更多,做你想做的。\nE浏览附近感兴趣的群体,并加入他们。"]), 16 PageItemModel (dict: ["imageName":"moreInspir", "title":"更多灵感,更多思路", "detail":"遇见更多志同道合者。\n并与他们保持联系。"]), 17 PageItemModel (dict: ["imageName":"moreSimpler", "title":"更加简洁", "detail":"比以前更加简洁\n为你打造更加便捷的使用体验,更加轻松的找到你想要的。"]) 18 ] 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 23 // Do any additional setup after loading the view. 24 registerCell() 25 setBtn() 26 27 pageControler.numberOfPages = pages.count + 1 28 } 29 30 fileprivate func registerCell () { 31 collectionView.register(UINib (nibName: "GuideViewControllerCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "GuideViewControllerCollectionViewCell") 32 collectionView.register(UINib (nibName: "GuideViewControllerFinshCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "GuideViewControllerFinshCollectionViewCell") 33 } 34 35 fileprivate func setBtn() { 36 skipBtn.addTarget(self, action: #selector(pageSkipHandler(sender:)), for: .touchUpInside) 37 38 continueBtn.addTarget(self, action: #selector(pageSkipHandler(sender:)), for: .touchUpInside) 39 } 40 41 @objc func pageSkipHandler(sender: UIButton) { 42 let indexPath: IndexPath! 43 44 if sender == continueBtn { 45 indexPath = IndexPath (row: pageControler.currentPage + 1, section: 0) 46 pageControler.currentPage += 1 47 } else { 48 indexPath = IndexPath (row: pages.count, section: 0) 49 pageControler.currentPage = pages.count 50 } 51 52 if pageControler.currentPage == pages.count { 53 moveItemRelativeToScreen(off: true) 54 } 55 56 self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) 57 } 58 59 func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { 60 61 let pageNum: Int = Int(targetContentOffset.pointee.x / view.frame.width) 62 pageControler.currentPage = pageNum 63 64 if pageNum == pages.count { 65 moveItemRelativeToScreen(off: true) 66 } else { 67 moveItemRelativeToScreen(off: false) 68 } 69 } 70 71 func moveItemRelativeToScreen(off: Bool) { 72 if off { 73 pageControlerBottom.constant = -54.0 74 shipBtnTop.constant = -100.0 75 continueBtnTop.constant = -100.0 76 skipBtn.isEnabled = false 77 continueBtn.isEnabled = false 78 } else { 79 pageControlerBottom.constant = 0 80 shipBtnTop.constant = 0 81 continueBtnTop.constant = 0 82 skipBtn.isEnabled = true 83 continueBtn.isEnabled = true 84 } 85 86 // layout subviews 87 UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { 88 self.view.layoutIfNeeded() 89 }, completion: nil) 90 } 91 92 override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) { 93 let indexPath = IndexPath (item: pageControler.currentPage, section: 0) 94 95 DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { 96 self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) 97 } 98 } 99 100 deinit { 101 print("\(self) has been deinit") 102 } 103 104 override func didReceiveMemoryWarning() { 105 super.didReceiveMemoryWarning() 106 // Dispose of any resources that can be recreated. 107 } 108 109 110 /* 111 // MARK: - Navigation 112 113 // In a storyboard-based application, you will often want to do a little preparation before navigation 114 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 115 // Get the new view controller using segue.destinationViewController. 116 // Pass the selected object to the new view controller. 117 } 118 */ 119 120 } 121 122 extension GuideViewController { 123 func numberOfSections(in collectionView: UICollectionView) -> Int { 124 return 1 125 } 126 127 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 128 return pages.count + 1 129 } 130 131 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 132 133 if indexPath.row == pages.count { 134 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GuideViewControllerFinshCollectionViewCell", for: indexPath) 135 136 return cell 137 } 138 139 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GuideViewControllerCollectionViewCell", for: indexPath) as! GuideViewControllerCollectionViewCell 140 141 let page = pages[indexPath.row] 142 143 // print("打印 pages 数组: \(pages)") 144 // print("打印 page Model: \(page.title) \(page.detail) \(page.imageName)") 145 146 cell.textLabel.text = page.title?.appendingFormat("%@", page.detail!) 147 148 return cell 149 } 150 151 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 152 return CGSize (width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height) 153 } 154 155 }
1 import UIKit 2 3 @objcMembers class PageItemModel: NSObject { 4 5 var imageName: String? 6 var title: String? 7 var detail: String? 8 9 init(dict: [String: Any]) { 10 super.init() 11 12 setValuesForKeys(dict) 13 } 14 15 override func setValue(_ value: Any?, forUndefinedKey key: String) { 16 // do SomeThing 17 } 18 }
怎么样成为程序员,学习和实践,日积月累...