swift--UIScrollView实例之启动导航页

该实例来自
http://www.swiftv.cn/secure/course/idfsulrn/learn#lesson/idfsulrn0.68083315640452980.2959634754809969
修改了里面的一点bug,做做笔记,以后直接拿来用。

1、声明全局变量

//声明一个UIScrollView对象
var scrollView: UIScrollView!

//声明一个UIPageControl对象,用于显示当前页索引
var pageControl: UIPageControl!

//声明一个UIButton对象, 用于跳转页面
var btn: UIButton!

//记录button的位置
var origin: CGFloat = 0

//记录是否第一次让按钮出现
var isFirst = true

2、初始化UI

func layoutUI() {
    //初始化页面跳转button
    btn = UIButton(frame: CGRect(x: 3*self.view.frame.width, y: self.view.frame.height, width: self.view.frame.width, height: 50))
    btn.backgroundColor = UIColor.orangeColor()
    btn.setTitle("立即进入", forState: UIControlState.Normal)
    btn.tintColor = UIColor.blackColor()
    btn.alpha = 0
    btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
    btn.addTarget(self, action: "btnClicked:", forControlEvents: UIControlEvents.TouchUpInside)
    //记录button的原始位置
    origin = btn.frame.origin.y
    
    //初始化导航页面索引
    pageControl = UIPageControl()
    pageControl.center = CGPointMake(self.view.frame.width/2, self.view.frame.height-30)
    pageControl.currentPageIndicatorTintColor = UIColor.redColor()
    pageControl.pageIndicatorTintColor = UIColor.whiteColor()
    pageControl.numberOfPages = 4
    pageControl.addTarget(self, action: "scrollViewDidEndDecelerating:", forControlEvents: UIControlEvents.ValueChanged)
    
    //初始化scrollview
    scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
    scrollView.bounces = false;
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.pagingEnabled = true
    scrollView.delegate = self
    scrollView.contentSize = CGSizeMake(4*self.view.frame.width, 0)
    //将scrollview和pageControl添加到页面上,注意顺序
    self.view.addSubview(scrollView)
    self.view.addSubview(pageControl)
}

3、初始化数据

func initData() {
    for var i = 0; i < 4; i++ {
        //声明一个image,用于呈现图片
        let image = UIImage(named: "Welcome\(i+1)")
        //声明一个imageview,用于装载图片
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollView.frame.width, height: scrollView.frame.height))
        //给imageview设置图片
        imageView.image = image
        //记录当前imageview的frame值
        var frame = imageView.frame
        //记录x坐标
        frame.origin.x = CGFloat(i)*frame.size.width
        //设置当前imageview的坐标
        imageView.frame = frame
        //添加到父组件
        scrollView.addSubview(imageView)
    }
}

4、监听scrollview滚动事件

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    //获取scrollview偏移量,得到整数值
    let index = Int(scrollView.contentOffset.x / self.view.frame.width)
    //设置索引
    pageControl.currentPage = index
    //当滑动到最后一个页面时,出现按钮
    if index == 3 && isFirst {
        //设为false,之后不再允许该段代码,防止动画重复
        isFirst = false
        //设置按钮最终出现的位置
        origin -= 100
        //添加button到父组件上,注意不能添加到imageview上,否则不能点击,原因不详。
        scrollView.addSubview(btw)
        //设置动画,关于动画,请看swift动画一节
        UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.btn.frame.origin.y = self.origin
            self.btn.alpha = 1
            }, completion: { (finish) -> Void in
                //还原button原始位置的值
                self.origin += 100
        })
    }
}

5、button点击事件

func btnClicked(sender: UIButton) {
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vc = sb.instantiateViewControllerWithIdentifier("second") as! SecondViewController
    vc.set(self)
    //这尼玛绝对是新手最纠结的地方,对于安卓来说跳转页面后向干掉自己,可以调用this.finish()或者去配置文件设置启动模式。
    //但是ios没有这些,网上也找不到更多地资料,我现在了解的跳转后干掉自己的方法就是替换根控制器。也是网上出现较多的答案。
    UIApplication.sharedApplication().delegate?.window!!.rootViewController = vc;
          //单用此方法,不能干掉自己
//        self.presentViewController(vc, animated: true, completion: nil)
          //这个需要和addChildViewController()配合使用才有效,通常自定义tabbar的时候用
//        self.removeFromParentViewController()
}

6、最后在AppDelegate中设置是否显示启动导航页

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    //关于用户首选项,“请查看Swift之数据管理(一)” 一节
    if !NSUserDefaults.standardUserDefaults().boolForKey("firstLaunch") {
        self.window?.rootViewController = ViewController()
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstLaunch")
    } else {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("second")
        self.window?.rootViewController = vc
    }
    //重要!! 使界面显示出来
    self.window?.makeKeyAndVisible()
    return true
}

至此,导航页结束!!!

你可能感兴趣的:(swift--UIScrollView实例之启动导航页)