GuideViewController.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // GuideViewController.swift
  3. // BingHaoBang
  4. //
  5. // Created by zhangjidong on 16/3/31.
  6. // Copyright © 2016年 Zjdboy. All rights reserved.
  7. //
  8. import UIKit
  9. class GuideViewController: UIViewController,UIScrollViewDelegate {
  10. var scrollView = UIScrollView()
  11. var pageControl = UIPageControl()
  12. var btn = UIButton()
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. //配置pageControl
  16. pageControl.center = CGPointMake(self.view.frame.width/2, self.view.frame.height-30)
  17. pageControl.currentPageIndicatorTintColor = UIColor.redColor()
  18. pageControl.pageIndicatorTintColor = UIColor.whiteColor()
  19. pageControl.numberOfPages = 4
  20. pageControl.addTarget(self, action: #selector(UIScrollViewDelegate.scrollViewDidEndDecelerating(_:)), forControlEvents: UIControlEvents.ValueChanged)
  21. //配置scrollView
  22. scrollView.frame = self.view.bounds
  23. scrollView.contentSize = CGSizeMake(4*self.view.frame.width, 0)
  24. scrollView.pagingEnabled = true
  25. scrollView.bounces = false
  26. scrollView.showsHorizontalScrollIndicator = false
  27. scrollView.delegate = self
  28. self.view.addSubview(scrollView)
  29. for i in 0 ..< 4
  30. {
  31. let image = UIImage(named: "Guide_\(i+1)")
  32. let imageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
  33. imageView.image = image
  34. var frame = imageView.frame
  35. frame.origin.x = CGFloat(i)*frame.size.width
  36. imageView.frame = frame
  37. scrollView.addSubview(imageView)
  38. self.view.addSubview(pageControl)
  39. }
  40. }
  41. /**
  42. 按钮点击事件
  43. - parameter button: UIButton
  44. */
  45. func buttonClick(button:UIButton)
  46. {
  47. //跳转页面
  48. self.presentViewController(MainTabBarController(), animated: true, completion: nil)
  49. }
  50. /**
  51. 导航处理
  52. - parameter scrollView: UIScrollView
  53. */
  54. func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
  55. let index = Int(scrollView.contentOffset.x / self.view.frame.size.width) //获取当前页数
  56. pageControl.currentPage = index
  57. //在这里添加按钮的渐入效果,当页面滑到第4页时出现
  58. if(index == 3)
  59. {
  60. self.btn.frame = CGRectMake(3*self.view.frame.width, self.view.frame.height, self.view.frame.width, 50)
  61. self.btn.setTitle("开启旅程", forState: UIControlState.Normal)
  62. self.btn.titleLabel?.font = UIFont.systemFontOfSize(20)
  63. self.btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
  64. self.btn.backgroundColor = UIColor.orangeColor()
  65. self.btn.alpha = 0
  66. self.btn.addTarget(self, action: #selector(GuideViewController.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
  67. UIView.animateWithDuration(1.5, delay: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
  68. self.btn.frame = CGRectMake(3*self.view.frame.width, self.view.frame.height-100, self.view.frame.width, 50)
  69. self.btn.alpha = 1
  70. //注意把按钮添加到scrollView上,不要添加到imageView上,会无法点击
  71. self.scrollView.addSubview(self.btn)
  72. }, completion: nil)
  73. }
  74. }
  75. override func didReceiveMemoryWarning() {
  76. super.didReceiveMemoryWarning()
  77. // Dispose of any resources that can be recreated.
  78. }
  79. }