[IOS]ScrollView & PageControl 制作引导页

ScrollView & PageControl 制作引导页

此空间用于制作app的开始画面。

在实际做app的时候,可能用纯代码实现会更加方便,但需要的能力也更大,特别是对布局的掌控!此能力需要看更多的开源代码来学习!不能太依赖StoryBoard。

(Demo)代码实现:

先布局好ScrollView,PageControl,以及ScrollView的Delegate。
//
//  ViewController.swift
//  demo
//
//  Created by 颜泽鑫 on 4/10/16.
//  Copyright © 2016 颜泽鑫. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {
    // 已连线。
    @IBOutlet weak var ScrollView: UIScrollView!
    @IBOutlet weak var PageControl: UIPageControl!
    // 不连线。
    var btn = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        ScrollView.frame = self.view.frame
        // 确定内容的宽度,也就是页数。!
        ScrollView.contentSize = CGSizeMake(4 * self.view.frame.width, 0)
        ScrollView.pagingEnabled = true
        ScrollView.showsHorizontalScrollIndicator = false
        // 此处不能使用storyborad来添加image。
        // 因为,此处是每张图都要有一个imageView对应,而不是一个ImageView对应各种图。
        for var i=0;i<4;i++ {
            let image = UIImage(named: "\(i+1)")
            let imageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
            imageView.image = image
            var frame = imageView.frame
            frame.origin.x = CGFloat(i)*frame.size.width
            imageView.frame = frame
            ScrollView.addSubview(imageView)
            // 这里一定要写这条代码!
            // 因为每新增一个image都会把原来的PageControl盖住
            self.view.addSubview(PageControl)
        }
    }

    func buttonClick(button:UIButton)
    {
        let controller = UIAlertController(title: "Wellcome to C++!", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
        let CancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
        controller.addAction(CancelAction)
        self.presentViewController(controller, animated: true, completion: nil)
    }

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        // 通过x的偏移量来计算页数。
        let index = Int(scrollView.contentOffset.x / self.view.frame.size.width)    //获取当前页数
        PageControl.currentPage = index
        PageControl.alpha = 1
        //在这里添加按钮的渐入效果,当页面滑到第4页时出现
        if(index == 3) {
            self.PageControl.alpha = 0
            // 此处3*self.view.frame.width3,
            // 指的是对于ScrollView上的x位置。
            self.btn.frame = CGRectMake(3*self.view.frame.width, self.view.frame.height, self.view.frame.width, 50)
            self.btn.setTitle("开启C++之旅", forState: UIControlState.Normal)
            self.btn.titleLabel?.font = UIFont.systemFontOfSize(20)
            self.btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
            // 此处的RGB都是小数值,需要把RGB值除以255
            self.btn.backgroundColor = UIColor(red: CGFloat(0), green: CGFloat(0.3), blue: CGFloat(1), alpha: CGFloat(1))
            self.btn.alpha = 0
            self.btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            UIView.animateWithDuration(1.5, delay: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                self.btn.frame = CGRectMake(3*self.view.frame.width, self.view.frame.height-100, self.view.frame.width, 50)
                self.btn.alpha = 1
                //注意把按钮添加到scrollView上,不要添加到imageView上,会无法点击
                self.ScrollView.addSubview(self.btn)
                }, completion: nil)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

反思:此Demo遇到了大量不能使用StoryBoard来增加控件的情况,我个人理解是,因为某些控件只有在特定的情况下才能显现和使用,如果把它直接放在StoryBoard的话,就可能会出现无法预计的效果。
ScollView的空间实际长度(contentsize)是界面的3倍,也就是说,实际上他是一个控件在不同区域的显示,所以不能在storyboard中添加三个imageView来显示不同的图片。
这也提醒我,除了学会如何用StoryBoard以外,还会学会如何手写纯代码实现相应的功能。

如何只显示一次引导页:

简单地说,就是在AppDelegate里面判断是否是第一次打开此app。
    func buttonClick(button:UIButton)
    {
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let secondVC = sb.instantiateViewControllerWithIdentifier("second")
        self.presentViewController(secondVC, animated: true, completion: nil)
    }
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        //判断App是否第一次启动
        if(!NSUserDefaults.standardUserDefaults().boolForKey("firstLaunch"))
        {
//            self.window?.rootViewController = ViewController()
            // self.window?.makeKeyAndVisible()
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstLaunch")
        }
        else
        {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let secondVC = sb.instantiateViewControllerWithIdentifier("second")
            self.window?.rootViewController = secondVC
            // self.window?.makeKeyAndVisible()
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstLaunch")
        }
        return true
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值