該實例來自
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
}
至此,導航頁結束!!!