iOS App初次啟動(dòng)時(shí)的用戶引導(dǎo)頁制作實(shí)例分享
應(yīng)用程序APP一般都有引導(dǎo)頁,引導(dǎo)頁可以作為操作指南指導(dǎo)用戶熟悉使用;也可以展現(xiàn)給用戶,讓用戶了解APP的功能作用。引導(dǎo)頁制作簡(jiǎn)單,一般只需要一組圖片,再把圖片組展現(xiàn)出來就可以了。展示圖片組常用UIScrollView來分頁顯示,并且由UIPageControl頁面控制器控制顯示當(dāng)前頁。UIScrollView和UIPageControl搭配會(huì)更加完美地展現(xiàn)引導(dǎo)頁的功能作用。下面我們來看具體的實(shí)例:
我們用NSUserDefaults類來判斷程序是不是第一次啟動(dòng)或是否更新,在 AppDelegate.swift中加入以下代碼:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// 得到當(dāng)前應(yīng)用的版本號(hào)
let infoDictionary = NSBundle.mainBundle().infoDictionary
let currentAppVersion = infoDictionary!["CFBundleShortVersionString"] as! String
// 取出之前保存的版本號(hào)
let userDefaults = NSUserDefaults.standardUserDefaults()
let appVersion = userDefaults.stringForKey("appVersion")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// 如果 appVersion 為 nil 說明是第一次啟動(dòng);如果 appVersion 不等于 currentAppVersion 說明是更新了
if appVersion == nil || appVersion != currentAppVersion {
// 保存最新的版本號(hào)
userDefaults.setValue(currentAppVersion, forKey: "appVersion")
let guideViewController = storyboard.instantiateViewControllerWithIdentifier("GuideViewController") as! GuideViewController
self.window?.rootViewController = guideViewController
}
return true
}
在GuideViewController中,我們用UIScrollView來裝載我們的引導(dǎo)頁:
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 設(shè)為屏幕寬度的3倍(根據(jù)實(shí)際情況改變)
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)
// 給開始按鈕設(shè)置圓角
startButton.layer.cornerRadius = 15.0
// 隱藏開始按鈕
startButton.alpha = 0.0
}
// 隱藏狀態(tài)欄
override func prefersStatusBarHidden() -> Bool {
return true
}
}
最后我們讓GuideViewController遵循UIScrollViewDelegate協(xié)議,在這里判斷是否滑動(dòng)到最后一張以顯示進(jìn)入按鈕:
// MARK: - UIScrollViewDelegate
extension GuideViewController: UIScrollViewDelegate {
func scrollViewDidScroll(scrollView: UIScrollView) {
let offset = scrollView.contentOffset
// 隨著滑動(dòng)改變pageControl的狀態(tài)
pageControl.currentPage = Int(offset.x / view.bounds.width)
// 因?yàn)閏urrentPage是從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
}
}
}
}
在上面的代碼中,為了顯得自然我們給進(jìn)入按鈕加入了一點(diǎn)動(dòng)畫 :]
最終效果如下:
- 總結(jié)iOS App開發(fā)中控制屏幕旋轉(zhuǎn)的幾種方式
- iOS保存App中的照片到系統(tǒng)相冊(cè)或自建相冊(cè)的方法
- iOS App中調(diào)用相冊(cè)中圖片及獲取最近的一張圖片的方法
- 詳解在iOS App中自定義和隱藏狀態(tài)欄的方法
- safari調(diào)試iOS app web頁面的步驟
- iOS App開發(fā)中使cell高度自適應(yīng)的黑魔法詳解
- 詳解iOS App開發(fā)中Cookie的管理方法
- IOS App圖標(biāo)和啟動(dòng)畫面尺寸詳細(xì)介紹
- iOS開發(fā)中使app獲取本機(jī)通訊錄的實(shí)現(xiàn)代碼實(shí)例
- iOS開發(fā)教程之APP內(nèi)部切換語言的實(shí)現(xiàn)方法
相關(guān)文章
iOS實(shí)現(xiàn)手動(dòng)和自動(dòng)屏幕旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)手動(dòng)和自動(dòng)屏幕旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07iOS開發(fā)中常用的各種動(dòng)畫、頁面切面效果
這篇文章主要介紹了iOS開發(fā)中常用的各種動(dòng)畫、頁面切面效果 的相關(guān)資料,需要的朋友可以參考下2016-04-04基于iOS實(shí)現(xiàn)音樂震動(dòng)條效果
這篇文章主要為大家詳細(xì)介紹了基于iOS實(shí)現(xiàn)音樂震動(dòng)條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Flutter Widgets粘合劑CustomScrollView NestedScrollVie
這篇文章主要為大家介紹了Flutter Widgets粘合劑CustomScrollView NestedScrollView滾動(dòng)控件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11xcode8提交ipa失敗無法構(gòu)建版本問題的解決方案
xcode升級(jí)到xcode8后發(fā)現(xiàn)構(gòu)建不了新的版本。怎么解決呢?下面小編給大家?guī)砹藊code8提交ipa失敗無法構(gòu)建版本問題的解決方案,非常不錯(cuò),一起看看吧2016-10-10