[Swift通天遁地]9、拔劍吧-(15)搭建具備滑出、視差、3D變形等切換效果的引導頁

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-wyxvcwbe-hp.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目錄:[Swift]通天遁地Swiftgit

軟件通常包含一個引導頁面,向用戶形象的展現產品的主要特性。github

本文將演示搭建具備滑出、視差、3D變形等切換效果的引導頁。swift

GitHub項目:【ariok/BWWalkthrough】,下載並解壓文件。微信

【Pod】->選擇兩個文件:app

【BWWalkthroughViewController.swift】、編輯器

【BWWalkthroughPageViewController.swift】ide

將選擇的兩個文件,拖動到項目中。在彈出的文件導入確認窗口中,點擊【Finish】post

建立一個自定義的視圖控制器,用來製做三維變形的切換效果。ui

在項目文件夾上點擊鼠標右鍵,彈出右鍵菜單。

【New File->【Cocoa Touch->【Next】->

【Class】:CustomPageViewController

【Subclass of:UIViewController

【Language】:Swift

->Next->【Create】

點擊打開【CustomPageViewController.swift】

如今開始編寫代碼,實現三維變形的切換效果。 

 1 import UIKit
 2 
 3 //繼承已經安裝的第三方類庫。
 4 class CustomPageViewController: UIViewController,BWWalkthroughPage {
 5     
 6     //添加一個用於三維變形的圖像屬性。
 7     @IBOutlet var imageView:UIImageView?
 8     //添加一個用於三維變形的標籤對象
 9     @IBOutlet var titleLabel:UILabel?
10     //最後添加一個用於三維變形的子標題標籤對象。
11     @IBOutlet var textLabel:UILabel?
12     
13     override func viewDidLoad() {
14         super.viewDidLoad()
15 
16         // Do any additional setup after loading the view.
17     }
18     
19     //添加一個代理方法,用來監聽頁面滾動切換的事件。
20     func walkthroughDidScroll(_ position: CGFloat, offset: CGFloat)
21     {
22         //建立一個三維矩陣,該矩陣沒有縮放、旋轉、歪斜和透視等變形效果。
23         var tr = CATransform3DIdentity
24         //設置三維變形的景深效果,其值越接近0,景深效果就越強烈。
25         tr.m34 = -1/500.0
26         
27         //設置標題文字層的變形效果爲三維旋轉。
28         titleLabel?.layer.transform = CATransform3DRotate(tr, CGFloat(M_PI) * (1.0 - offset), 1, 1, 1)
29         //給子標題文字的層,添加三維旋轉的變形效果。
30         textLabel?.layer.transform = CATransform3DRotate(tr, CGFloat(M_PI) * (1.0 - offset), 1, 1, 1)
31         
32         //得到當前頁面的滾動偏移,
33         var tmpOffset = offset
34         //當偏移值大於1時
35         if tmpOffset > 1.0
36         {
37             //設置圖片向下方移動
38             tmpOffset = 1.0 + (1.0 - tmpOffset)
39         }
40         
41         imageView?.layer.transform = CATransform3DTranslate(tr, 0 , (1.0 - tmpOffset) * 200, 0)
42     }
43     
44     override func didReceiveMemoryWarning() {
45         super.didReceiveMemoryWarning()
46         // Dispose of any resources that can be recreated.
47     }
48 }

給圖像視圖的層添加三維移動的變形效果。

點擊資源文件夾【Assets.xcassets】,打開故事板文件【Main.storyboard

使用快捷鍵【Command】+D,複製當前選擇的視圖控制器,而後將它移動到適當的位置。

打開控件庫列表窗口,往故事板中添加一個按鈕控件,

做爲執行上一頁操做的按鈕。選擇視圖控制器,點擊身份檢查器圖標,進入身份設置面板。

【Class】:BWWalkthroughViewController輸入由第三方提供的類名。

設置視圖控制器在故事板中的惟一標誌符。

【Storyboard ID】:walk

點擊輔助編輯器圖標,打開輔助編輯器,

將故事板中的控件和類文件中的屬性進行鏈接。

在左側的項目導航區,打開視圖控制器的代碼文件【ViewController.swift】

如今開始編寫代碼,添加一個按鈕控件,當點擊按鈕時,彈出引導窗口。

 1 import UIKit
 2 
 3 //使類遵循引導視圖控制器代理協議,將使用該協議中的方法,監聽頁面的切換事件。
 4 class ViewController: UIViewController, BWWalkthroughViewControllerDelegate {
 5 
 6     override func viewDidLoad() {
 7         super.viewDidLoad()
 8         // Do any additional setup after loading the view, typically from a nib.
 9         
10         //添加一個按鈕,當用戶點擊該按鈕時,彈出引導窗口。
11         let goThrough = UIButton(frame: CGRect(x: 20, y: 40, width: 280, height: 40))
12         //將按鈕放置在根視圖的中心位置。
13         goThrough.center = self.view.center
14         //設置按鈕的背景顏色爲橙色
15         goThrough.backgroundColor = UIColor.orange
16         //同時設置按鈕在正常狀態下的標題文字。
17         goThrough.setTitle("Go Through", for: .normal)
18         //給按鈕控件綁定點擊事件。
19         goThrough.addTarget(self, action: #selector(ViewController.showWalkthrough), for: .touchUpInside)
20         
21         //設置根視圖的背景顏色爲橙色
22         self.view.backgroundColor = UIColor.orange
23         //並將按鈕控件添加到根視圖
24         self.view.addSubview(goThrough)
25     }
26     
27     //添加一個方法,用來響應按鈕的點擊事件。
28     @objc func showWalkthrough()
29     {
30         //從項目中加載指定名稱的故事板文。
31         let stb = UIStoryboard(name: "Main", bundle: nil)
32         //得到故事板中的指定標識符的引導視圖控制器。
33         let walkthrough = stb.instantiateViewController(withIdentifier: "walk") as! BWWalkthroughViewController
34         
35         //使用相同的方式,得到其餘四個視圖控制器。
36         let page_zero = stb.instantiateViewController(withIdentifier: "walk0")
37         let page_one = stb.instantiateViewController(withIdentifier: "walk1")
38         let page_two = stb.instantiateViewController(withIdentifier: "walk2")
39         let page_three = stb.instantiateViewController(withIdentifier: "walk3")
40         
41         //設置引導視圖控制器的代理,爲當前的視圖控制器對象。
42         walkthrough.delegate = self
43         //將其餘四個控制器,添加到引導視圖控制器。
44         walkthrough.addViewController(page_one)
45         walkthrough.addViewController(page_two)
46         walkthrough.addViewController(page_three)
47         walkthrough.addViewController(page_zero)
48         
49         //以模態的方式,打開引導視圖控制器
50         self.present(walkthrough, animated: true, completion: nil)
51     }
52     
53     //添加一個方法,用來監聽引導頁面發生切換的事件。
54     func walkthroughPageDidChange(_ pageNumber: Int)
55     {
56         print("Current Page \(pageNumber)")
57     }
58     
59     //添加一個方法,用來響應關閉按鈕的點擊事件
60     func walkthroughCloseButtonPressed()
61     {
62         self.dismiss(animated: true, completion: nil)
63     }
64     
65     override func didReceiveMemoryWarning() {
66         super.didReceiveMemoryWarning()
67         // Dispose of any resources that can be recreated.
68     }
69 }

繼續編寫代碼,使引導頁面在程序啓動後馬上打開。

在左側的項目導航區,打開應用程序的代理文件【AppDelegate.swift

 1 import UIKit
 2 
 3 @UIApplicationMain
 4 class AppDelegate: UIResponder, UIApplicationDelegate, BWWalkthroughViewControllerDelegate {
 5 
 6     var window: UIWindow?
 7 
 8     //使類遵循引導視圖控制器代理協議,將使用該協議中的方法,監聽頁面的切換事件。
 9     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: Any]?) -> Bool {
10         // Override point for customization after application launch.
11         
12         //從項目中讀取指定名稱的故事板文件。
13         let stb = UIStoryboard(name: "Main", bundle: nil)
14         //得到故事板中的,指定標識符的引導視圖控制器。
15         let walkthrough = stb.instantiateViewController(withIdentifier: "walk") as! BWWalkthroughViewController
16         //使用相同的方式,得到其餘的四個視圖控制器。
17         let page_zero = stb.instantiateViewController(withIdentifier: "walk0")
18         let page_one = stb.instantiateViewController(withIdentifier: "walk1")
19         let page_two = stb.instantiateViewController(withIdentifier: "walk2")
20         let page_three = stb.instantiateViewController(withIdentifier: "walk3")
21         
22         //設置引導視圖控制器的代理,爲當前的應用程序代理對象。
23         walkthrough.delegate = self
24         //將其餘四個控制器,添加到引導視圖控制器。
25         walkthrough.addViewController(page_one)
26         walkthrough.addViewController(page_two)
27         walkthrough.addViewController(page_three)
28         walkthrough.addViewController(page_zero)
29         
30         //設置當前窗口的根視圖控制器,爲引導頁視圖控制器。
31         self.window?.rootViewController = walkthrough
32         
33         return true
34     }
35 
36     //添加一個方法,用來監聽引導頁面發生切換的事件。
37     func walkthroughPageDidChange(_ pageNumber: Int)
38     {
39         print("Current Page \(pageNumber)")
40     }
41     
42     func applicationWillResignActive(_ application: UIApplication) {
43         // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
44         // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
45     }
46 
47     func applicationDidEnterBackground(_ application: UIApplication) {
48         // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
49         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
50     }
51 
52     func applicationWillEnterForeground(_ application: UIApplication) {
53         // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
54     }
55 
56     func applicationDidBecomeActive(_ application: UIApplication) {
57         // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
58     }
59 
60     func applicationWillTerminate(_ application: UIApplication) {
61         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
62     }
63 }
相關文章
相關標籤/搜索