iOS开发笔记(Swift)-通用App安装引导页的实现

时间:2022-10-21 09:31:43

之前一直做的项目都是基于OC开发的,最近开始尝试使用Swift语言来重写整个项目。

本篇文章主要是讲述如何使用Swift来实现常见的通用App安装引导界面。

效果预览:

iOS开发笔记(Swift)-通用App安装引导页的实现

实现思路:

主要是采用了UIScrollView作为容器,将其ContentSize的宽度设置为(引导页数量)*屏幕宽度,并将pagingEnabled设为true,隐藏滚动条。作为指示的UIPageControl与UIScrollView直接添加到self.view之上,并保存UiPageControl居于最上。

实现代码:

import UIKit

class YSYGuideViewController: UIViewController,UIScrollViewDelegate {
var scrollView:UIScrollView!
var pageControl:UIPageControl!
var startBtn:UIButton! override func viewDidLoad() {
super.viewDidLoad() // Do any additional setup after loading the view.
loadCustomLayout() } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} //scrollview委托
func scrollViewDidScroll(scrollView: UIScrollView) {
var offset=scrollView.contentOffset
pageControl.currentPage=Int(offset.x/Yunshouyi.SCREEN_WIDTH)
if(pageControl.currentPage==2){
startBtn.hidden=false
}else{
startBtn.hidden=true
}
} //载入自定义布局
func loadCustomLayout(){
scrollView=UIScrollView(frame: CGRectMake(0, 0, Yunshouyi.SCREEN_WIDTH, Yunshouyi.SCREEN_HEIGHT))
scrollView.pagingEnabled=true
scrollView.showsHorizontalScrollIndicator=false
scrollView.showsVerticalScrollIndicator=false
scrollView.contentSize=CGSizeMake(3*Yunshouyi.SCREEN_WIDTH, Yunshouyi.SCREEN_HEIGHT)
for i in 1...4{
var image=UIImage(named: "Guideline-\(i)")
var imageView=UIImageView(image: image)
imageView.frame=CGRectMake(CGFloat((i-1))*Yunshouyi.SCREEN_WIDTH, 0, Yunshouyi.SCREEN_WIDTH, Yunshouyi.SCREEN_HEIGHT)
imageView.contentMode = UIViewContentMode.ScaleToFill
scrollView.addSubview(imageView) }
pageControl=UIPageControl(frame: CGRectMake(Yunshouyi.SCREEN_WIDTH/2-100, Yunshouyi.SCREEN_HEIGHT-100, 200, 100))
startBtn=UIButton(frame: CGRectMake(Yunshouyi.SCREEN_WIDTH/2-60, Yunshouyi.SCREEN_HEIGHT/2+50, 120, 35)) pageControl.numberOfPages=3;
pageControl.currentPage=0 startBtn.setTitle("马上体验", forState: UIControlState.Normal)
startBtn.setBackgroundImage(UIImage(named: "StartButton"), forState: UIControlState.Normal)
startBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
startBtn.addTarget(self, action: "guideOver", forControlEvents: UIControlEvents.TouchUpInside)
startBtn.hidden=true scrollView.bounces=false
scrollView.delegate=self
self.view.addSubview(scrollView)
self.view.addSubview(pageControl)
self.view.addSubview(startBtn) } //开始使用app
func guideOver(){
var mainVC=UINavigationController(rootViewController: UITableViewController())
self.presentViewController(mainVC, animated: true, completion: nil) } override func prefersStatusBarHidden() -> Bool {
return true
} }