[iOS] App引导页的简单实现 (Swift 2)
2016-04-03 01:40
429 查看
转载请注明出处:http://www.jianshu.com/p/024dd2d6e6e6#
已更新至 Xcode7.2、Swift2.1
在第一次打开App或者App更新后通常用引导页来展示产品特性
我们用
在
最后我们让
在上面的代码中,为了显得自然我们给进入按钮加入了一点动画 :]
最终效果如下:
![](http://upload-images.jianshu.io/upload_images/179888-648bc09efcd64289.gif?imageMogr2/auto-orient/strip)
GuideScreenshot.gif
Github地址:https://github.com/GuiminChu/JianshuExample
文/老初(简书作者)
原文链接:http://www.jianshu.com/p/024dd2d6e6e6
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
已更新至 Xcode7.2、Swift2.1
在第一次打开App或者App更新后通常用引导页来展示产品特性
我们用
NSUserDefaults类来判断程序是不是第一次启动或是否更新,在
AppDelegate.swift中加入以下代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // 得到当前应用的版本号 let infoDictionary = NSBundle.mainBundle().infoDictionary let currentAppVersion = infoDictionary!["CFBundleShortVersionString"] as! String // 取出之前保存的版本号 let userDefaults = NSUserDefaults.standardUserDefaults() let appVersion = userDefaults.stringForKey("appVersion") let storyboard = UIStoryboard(name: "Main", bundle: nil) // 如果 appVersion 为 nil 说明是第一次启动;如果 appVersion 不等于 currentAppVersion 说明是更新了 if appVersion == nil || appVersion != currentAppVersion { // 保存最新的版本号 userDefaults.setValue(currentAppVersion, forKey: "appVersion") let guideViewController = storyboard.instantiateViewControllerWithIdentifier("GuideViewController") as! GuideViewController self.window?.rootViewController = guideViewController } return true }
在
GuideViewController中,我们用
UIScrollView来装载我们的引导页:
class GuideViewController: UIViewController { @IBOutlet weak var pageControl: UIPageControl! @IBOutlet weak var startButton: UIButton! private var scrollView: UIScrollView! private let numOfPages = 3 override func viewDidLoad() { super.viewDidLoad() let frame = self.view.bounds scrollView = UIScrollView(frame: frame) scrollView.pagingEnabled = true scrollView.showsHorizontalScrollIndicator = false scrollView.showsVerticalScrollIndicator = false scrollView.scrollsToTop = false scrollView.bounces = false scrollView.contentOffset = CGPointZero // 将 scrollView 的 contentSize 设为屏幕宽度的3倍(根据实际情况改变) scrollView.contentSize = CGSize(width: frame.size.width * CGFloat(numOfPages), height: frame.size.height) scrollView.delegate = self for index in 0..<numOfPages { // 这里注意图片的命名 let imageView = UIImageView(image: UIImage(named: "GuideImage\(index + 1)")) imageView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height: frame.size.height) scrollView.addSubview(imageView) } self.view.insertSubview(scrollView, atIndex: 0) // 给开始按钮设置圆角 startButton.layer.cornerRadius = 15.0 // 隐藏开始按钮 startButton.alpha = 0.0 } // 隐藏状态栏 override func prefersStatusBarHidden() -> Bool { return true } }
最后我们让
GuideViewController遵循
UIScrollViewDelegate协议,在这里判断是否滑动到最后一张以显示进入按钮:
// MARK: - UIScrollViewDelegate extension GuideViewController: UIScrollViewDelegate { func scrollViewDidScroll(scrollView: UIScrollView) { let offset = scrollView.contentOffset // 随着滑动改变pageControl的状态 pageControl.currentPage = Int(offset.x / view.bounds.width) // 因为currentPage是从0开始,所以numOfPages减1 if pageControl.currentPage == numOfPages - 1 { UIView.animateWithDuration(0.5) { self.startButton.alpha = 1.0 } } else { UIView.animateWithDuration(0.2) { self.startButton.alpha = 0.0 } } } }
在上面的代码中,为了显得自然我们给进入按钮加入了一点动画 :]
最终效果如下:
![](http://upload-images.jianshu.io/upload_images/179888-648bc09efcd64289.gif?imageMogr2/auto-orient/strip)
GuideScreenshot.gif
Github地址:https://github.com/GuiminChu/JianshuExample
文/老初(简书作者)
原文链接:http://www.jianshu.com/p/024dd2d6e6e6
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- swift系统自带二维码的生成
- Swift 基础语法
- Swift中的可选类型(Optional)以及?和!的用法详解
- 计时器Timer
- Swift获取沙盒中目录的一个分类
- Swift里封装AFNetworking
- swift segment实现页面切换
- Swift 学习记录(5)
- Swift语言精要 - Operator(运算符重载)
- 用Swift完成不同View Controller之间的切换
- swift与OC混编需要注意的小问题
- Swift 通知的使用(十一)
- Swift延迟加载的一种用途
- Swift延迟加载的一种用途
- Swift延迟加载的一种用途
- Swift语言精要 - Operator(运算符重载)
- Swift语言精要 - 扩展(Extension)
- Swift语言精要 - 属性
- Swift中的指针类型
- Swift利用闭包(closure)来实现传值-->前后两个控制器的反向传值