最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS橫豎屏旋轉(zhuǎn)內(nèi)容總結(jié)

 更新時(shí)間:2018年01月31日 14:42:30   投稿:laozhang  
本篇文章給大家總結(jié)了iOS橫豎屏旋轉(zhuǎn)的相關(guān)內(nèi)容以及實(shí)現(xiàn)的代碼分享,有需要的朋友參考下吧。

一、前言

Swift版本 4.0

Xcode版本 9.2

以前接觸到的項(xiàng)目需求中,幾乎都是全豎屏展現(xiàn)界面,所以我也來得省事,直接在TARGETS中的界面方向選項(xiàng)中只勾選豎屏,這樣就滿足了需求。

但最近的項(xiàng)目中,產(chǎn)品突然增加了一個(gè)需求,需要部分界面支持旋轉(zhuǎn),這才來研究了一下屏幕旋轉(zhuǎn)的問題!

需要緊急解決問題的道友直接看3.3

二、屏幕旋轉(zhuǎn)相關(guān)知識(shí)

2.1 三個(gè)方向的理解和聯(lián)系

UIDeviceOrientation: 設(shè)備方向

public enum UIDeviceOrientation : Int {
 case unknown
 case portrait // 設(shè)備vertically方向, home鍵在下方
 case portraitUpsideDown // 設(shè)備vertically方向, home鍵在上方
 case landscapeLeft // 設(shè)備horizontally方向, home鍵在右方
 case landscapeRight // 設(shè)備horizontally方向, home鍵在左方
 case faceUp // 設(shè)備flat方向, 屏幕朝上
 case faceDown // 設(shè)備flat方向, 屏幕朝下
}

 

從設(shè)備方向的命名就能看出來這個(gè)枚舉的含義,這里指的是物理設(shè)備(即iPhone)的方向。

UIInterfaceOrientation: 界面方向

public enum UIInterfaceOrientation : Int {
 case unknown
 case portrait
 case portraitUpsideDown
 case landscapeLeft
 case landscapeRight
}

而界面方向指屏幕中顯示內(nèi)容的方向,它的方向和Home鍵的方向是一致的。仔細(xì)觀察一下屏幕旋轉(zhuǎn)就能理解UIDeviceOrientation和UIInterfaceOrientation了,我們把手機(jī)轉(zhuǎn)向左邊,可以看到界面隨之才轉(zhuǎn)向右邊。

UIInterfaceOrientationMask: 是用來控制允許轉(zhuǎn)向的方向,對應(yīng)UIInterfaceOrientation

public struct UIInterfaceOrientationMask : OptionSet {
 public init(rawValue: UInt)
 public static var portrait: UIInterfaceOrientationMask { get }
 public static var landscapeLeft: UIInterfaceOrientationMask { get }
 public static var landscapeRight: UIInterfaceOrientationMask { get }
 public static var portraitUpsideDown: UIInterfaceOrientationMask { get }
 public static var landscape: UIInterfaceOrientationMask { get }
 public static var all: UIInterfaceOrientationMask { get }
 public static var allButUpsideDown: UIInterfaceOrientationMask { get }
}

2.2 觀察屏幕旋轉(zhuǎn)并作出響應(yīng)

2.2.1 觀察設(shè)備方向并響應(yīng)

// 沒有生成通知
if !UIDevice.current.isGeneratingDeviceOrientationNotifications {
 // 生成通知
  UIDevice.current.beginGeneratingDeviceOrientationNotifications()
}
// 鎖定豎屏,依然有效,例如faceUp.
NotificationCenter.default.addObserver(self, 
          selector: #selector(handleDeviceOrientationChange(notification:)),       name:NSNotification.Name.UIDeviceOrientationDidChange,
          object: nil)
@objc private func handleDeviceOrientationChange(notification: Notification) {
 // 獲取設(shè)備方向
 let orientation = UIDevice.current.orientation
 switch orientation {
  case .landscapeRight:
   // iOS8之后,橫屏UIScreen.main.bounds.width等于豎屏?xí)r的UIScreen.main.bounds.height
   print(UIScreen.main.bounds.width)
   print("landscapeRight")
  default: break
 }
}

注銷

deinit {
 NotificationCenter.default.removeObserver(self)
 UIDevice.current.endGeneratingDeviceOrientationNotifications()
}

2.2.2 觀察界面方向并響應(yīng)

和上面類似不過觀察的name為

// 鎖定豎屏,無效,通知方法不會(huì)觸發(fā)
NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
NSNotification.Name.UIApplicationDidChangeStatusBarOrientation

獲取界面方向

let statusBarOrientation = UIApplication.shared.statusBarOrientation

2.2.3 建議

這里建議監(jiān)聽界面方向,原因有二:

監(jiān)聽設(shè)備方向,會(huì)返回多個(gè)方向,例如portrait和faceUp不沖突。

監(jiān)聽設(shè)備方向,上面提到,先是設(shè)備旋轉(zhuǎn),隨之界面旋轉(zhuǎn),這里就有一個(gè)問題,我們操作界面時(shí),可能界面還沒有旋轉(zhuǎn)。

三、問題解決實(shí)戰(zhàn)

需要實(shí)現(xiàn)部分界面可旋轉(zhuǎn),部分界面鎖定豎屏,首先我們需要配置TARGETS中的Device Orientation,這里是總開關(guān),默認(rèn)勾選了如圖方向:

如果你確定整個(gè)項(xiàng)目只有豎屏,直接只勾選Protrait完事,不過像我現(xiàn)在這樣,可能突然一個(gè)需求改變就不得不繼續(xù)適配,哈哈。

這里的配置不要和代碼控制的方向相沖突,不然會(huì)引發(fā)奔潰。

3.1 控制屏幕旋轉(zhuǎn)的函數(shù)

// 默認(rèn)為true
override var shouldAutorotate: Bool {
 return true
}
// 支持的旋轉(zhuǎn)方向
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
 return .landscapeLeft
}
// 模態(tài)切換的默認(rèn)方向
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
 return .landscapeRight
}

這三個(gè)屬性都重寫的UIViewController的屬性。哎,看到模態(tài)切換,這里再給自己挖坑一個(gè),以前研究了一會(huì)模態(tài)切換,只不過沒寫成總結(jié),后面會(huì)寫出來(:。

并且這三個(gè)方法會(huì)受到控制器層級的影響,也就是如果當(dāng)前控制器配置支持旋轉(zhuǎn),如果他的導(dǎo)航控制器,乃至Tabbar控制器不支持旋轉(zhuǎn),當(dāng)前控制器的配置也不會(huì)生效。

3.2 不同根控制器情況下的解決

核心問題: 需要旋轉(zhuǎn)的界面是少數(shù),大多界面需要鎖定豎屏。

3.2.1 根控制器為UIViewController

對應(yīng)Demo配置:

這種情況的APP可以說是非常少了,不過還是對后面的情況有所幫助。

設(shè)置BaseVC,在其中的配置鎖定豎屏:

class BaseVC: UIViewController {
 override var shouldAutorotate: Bool {
  return false
 }  
 override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  return .portrait
 }
 override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
  return .portrait
 }  
 override func viewDidLoad() {
  super.viewDidLoad()
 } 
}

然后其余控制器繼承BaseVC,需要旋轉(zhuǎn)的控制器單獨(dú)再次重寫方法。

3.2.2 根控制器為UINavigationController

對應(yīng)Demo配置:

我們可以獲取到當(dāng)前顯示層級的控制器,并拿出它的屬性賦給UINavigationController

class BaseNavC: UINavigationController {
 override var shouldAutorotate: Bool {
  return self.viewControllers.last?.shouldAutorotate ?? false
 }
 override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  return self.viewControllers.last?.supportedInterfaceOrientations ?? .portrait
 }
 override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
  return self.viewControllers.last?.preferredInterfaceOrientationForPresentation ?? .portrait
 }
 override func viewDidLoad() {
  super.viewDidLoad()
 }
}

3.2.3 根控制器為UITabBarController

對應(yīng)Demo配置:

class BaseTabBarC: UITabBarController {
 override var shouldAutorotate: Bool {
  return self.selectedViewController?.shouldAutorotate ?? false
 }
  
 override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  return self.selectedViewController?.supportedInterfaceOrientations ?? .portrait
 }
  
 override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
  return self.selectedViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
 }
  
 override func viewDidLoad() {
  super.viewDidLoad()
 }
}

同理,我們只需要獲取當(dāng)前選中的控制器的配置賦給UITabBarController,這樣一層一層就配置好了!

3.3 最簡單的實(shí)現(xiàn)方式

對應(yīng)Demo配置:

在查詢屏幕旋轉(zhuǎn)相關(guān)資料的時(shí)候我發(fā)現(xiàn)屏幕旋轉(zhuǎn)時(shí)會(huì)最后調(diào)用Appdelegate中的:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) 
-> UIInterfaceOrientationMask {
}

然后我立馬想到一個(gè)超級簡單的方法,那就是定義一個(gè)全局變量或者緩存一個(gè)bool值來進(jìn)行判斷,如下:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) 
-> UIInterfaceOrientationMask {
 if isAllowAutorotate {
  return [.portrait, .landscapeLeft, .landscapeRight]
 }
 else {
  return .portrait
 }
}

然后默認(rèn)isAllowAutorotate這個(gè)全局變量為false,在需要旋轉(zhuǎn)的控制器中:

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)
  isAllowAutorotate = false
 }
  
 override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(animated)
  isAllowAutorotate = true
 }
}

這樣就不用麻煩的去搞那些繼承什么的了!

四、后記和Demo

https://github.com/swordjoy/ScreenRotationDemo

以上就是本次小編整理的全部內(nèi)容,感謝大家的支持,如果還有任何相關(guān)問題可以在下方的留言區(qū)討論。

相關(guān)文章

  • iOS正確監(jiān)聽手機(jī)靜音鍵和側(cè)邊音量鍵的方法示例

    iOS正確監(jiān)聽手機(jī)靜音鍵和側(cè)邊音量鍵的方法示例

    這篇文章主要給大家介紹了關(guān)于iOS正確監(jiān)聽手機(jī)側(cè)邊音量鍵的相關(guān)資料,并且給大家分享了ios監(jiān)聽靜音鍵的示例代碼,文中介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-11-11
  • iOS10 適配以及Xcode8配置總結(jié)

    iOS10 適配以及Xcode8配置總結(jié)

    這篇文章主要介紹了iOS10 適配以及Xcode8配置總結(jié)的相關(guān)資料,本文通過圖文并茂的形式給大家介紹,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • iOS之加載Gif圖片的方法

    iOS之加載Gif圖片的方法

    本篇文章主要介紹了iOS之加載Gif圖片,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • iOS 指紋解鎖驗(yàn)證TouchID功能

    iOS 指紋解鎖驗(yàn)證TouchID功能

    這篇文章主要介紹了iOS 指紋解鎖驗(yàn)證TouchID功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • iOS模糊效果的實(shí)現(xiàn)方法

    iOS模糊效果的實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了iOS模糊效果的實(shí)現(xiàn)方法,利用系統(tǒng)的CoreImage濾鏡、UIImage ImageEffects分類和UIVisualEffectView實(shí)現(xiàn)模糊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS如何裁剪圓形頭像

    iOS如何裁剪圓形頭像

    這篇文章主要介紹了iOS如何裁剪圓形頭像的方法,如何為圓形頭像加邊框,如何進(jìn)行截圖操作,感興趣的小伙伴們可以參考一下
    2016-04-04
  • iOS中Runtime的幾種基本用法記錄

    iOS中Runtime的幾種基本用法記錄

    RunTime顧名思義運(yùn)行時(shí),就是系統(tǒng)在運(yùn)行的時(shí)候的一些機(jī)制,最主要的是消息機(jī)制。下面這篇文章主要給大家介紹了關(guān)于iOS中Runtime的幾種基本用法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • 利用iOS手勢與scrollView代理實(shí)現(xiàn)圖片的放大縮小

    利用iOS手勢與scrollView代理實(shí)現(xiàn)圖片的放大縮小

    這篇文章主要介紹了利用iOS的手勢、scrollView代理來實(shí)現(xiàn)圖片放大縮小的方法,文中通過示例代碼介紹的很詳細(xì),相信對各位iOS開發(fā)者們來說具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-01-01
  • 利用iOS開發(fā)實(shí)現(xiàn)翻轉(zhuǎn)撲克牌動(dòng)畫的方法

    利用iOS開發(fā)實(shí)現(xiàn)翻轉(zhuǎn)撲克牌動(dòng)畫的方法

    這篇文章主要給大家介紹了關(guān)于利用iOS開發(fā)實(shí)現(xiàn)翻撲克牌動(dòng)畫的方法,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • iOS開發(fā)項(xiàng)目- 基于WebSocket的聊天通訊(1)

    iOS開發(fā)項(xiàng)目- 基于WebSocket的聊天通訊(1)

    這篇文章主要介紹了iOS開發(fā)項(xiàng)目- 基于WebSocket的聊天通訊,WebSocket是web通信方式的一種,有需要的可以了解一下。
    2016-11-11

最新評論

屏东县| 福清市| 清远市| 平陆县| 六安市| 志丹县| 土默特右旗| 西乌| 涡阳县| 凤翔县| 潜江市| 金溪县| 涿鹿县| 安阳县| 仁寿县| 扶沟县| 崇左市| 民乐县| 军事| 宝坻区| 微博| 北票市| 台州市| 龙海市| 礼泉县| 庄河市| 惠州市| 蒲城县| 黄龙县| 翼城县| 翁源县| 青铜峡市| 彰化县| 云林县| 图们市| 包头市| 临沭县| 沽源县| 娄底市| 门头沟区| 砀山县|