SlideShare ist ein Scribd-Unternehmen logo
1 von 24
如何支援 Mac OS X
 的高解析度模式
   楊維中 a.k.a zonble
萬惡的根源
開發環境

• MacBook Pro 2012
• 沒有新機器怎麼辦?
• 使用模擬環境
模擬環境
模擬環境
模擬環境




Quartz Debug
模擬環境
模擬環境
如何支援?


• 使用兩倍圖
• 改用向量圖形
使用兩倍圖
• 跟 iOS 一樣
• 所有用到的圖片
 的   名加上
 @2x,然後加到
使用向量圖
• 向量圖     格式很
 多:Adobe
 Illustrator、
 Corel Draw、
 SVG…
• Mac OS X 系統
 支援的向量格
Core Animation

• CALayer 必須要手動調整屬性,才有辦
 法支援 Retina Display

• contents 直接給 @2x 圖片是沒用的
• 必須手動設定 contentScale
iOS 上面
• aLayer. contentsScale = [UISCreen
  mainScreen].scale
• 反正一般來說,我們的 UI 只會出現在
  mainScreen 上。雖然可以外接投影機,
  使用另外一個 UIScreen,但是一般不會
  有人去寫        種 App
Mac OS X
•   CALayer 不一定會出現在 個 Screen 裡頭,而是要由
    目前 layer 所在的 Window 決定是否要使用高解析度

    •   詢問 NSWindow 的 -backingScaleFactor

•   Window 還可以在不同 Screen (主螢幕、延伸螢幕)
    之間移動

    •   contentsScale 需要隨時改變

•   還有…向下相容,你應該不會只支援 10.7.4 以上的系
    統
改變解析度的時機
• Layer 被放到某個 Window 的時候
• 因為 Window 移動而造成 Window 的
 scale 改變的時候(Notification)

• 以下範例是假設 rootLayer   個物件被擺
 在某個 NSView 裡頭
Layer 被放到某個

- (void)viewDidMoveToWindow {
   if ([rootLayer respondsToSelector:@selector(contentsScale)]) {
// 舊版的 OS 的 CALayer 不一定會有 contentsScale
// 舊版的 OS 的 Window 也不一定會有 backingScaleFactor…
        CGFloat scale = [(id)[self window] backingScaleFactor];
        [(id)rootLayer setContentsScale:scale];
    }
}
訂閱 Window 移動而
• 訂閱
 NSWindowDidChangeBackingPropertiesN
 otification
• 但因為舊版系統沒有,所以直接訂閱字
 串 @”
 NSWindowDidChangeBackingPropertiesN
 otification”
訂閱 Window 移動而

• 在 viewDidMoveToWindow 訂閱
• 在 removeFromSuperview 取消
• 我們先來把剛剛那段修改 contentsScale
 的程式拆出來
拆出來的 private
- (void)_updateContentScale {
   if (![self window]) {
       return;
   }

    if ([rootLayer respondsToSelector:@selector(contentsScale)]) {
        CGFloat scale = [(id)[self window] backingScaleFactor];
        [(id)rootLayer setContentsScale:scale];
    }
}
訂閱通知
- (void)viewDidMoveToWindow {
   [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(scale
DidChange:) name:@"NSWindowDidChangeBackingPropertiesNotification"
object:[self window]];
   [self _updateContentScale];
}

- (void)removeFromSuperview {
   [super removeFromSuperview];
   [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)scaleDidChange:(NSNotification *)n {
   [self _updateContentScale];
}
向下相容


• 用舊版的 SDK 編譯,compiler 會跟你抱
 怨沒有 contentsScale 與
 backingScaleFactor… 很討厭
為了向下相容弄出來

@interface NSObject (RetinaDisplay)
- (void)setContentsScale:(CGFloat)inScale;
- (CGFloat)backingScaleFactor;
@end
That’a all!

Weitere ähnliche Inhalte

Was ist angesagt?

苦集滅道:透過開發客製 Sketch Plug-in 改善產品設計流程
苦集滅道:透過開發客製 Sketch Plug-in  改善產品設計流程苦集滅道:透過開發客製 Sketch Plug-in  改善產品設計流程
苦集滅道:透過開發客製 Sketch Plug-in 改善產品設計流程Weizhong Yang
 
05 MapKit and Text Input
05 MapKit and Text Input05 MapKit and Text Input
05 MapKit and Text InputTom Fan
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器Chieh Kai Yang
 
Inspire dgt 網路技術分享_flash actionscritp class
Inspire dgt 網路技術分享_flash actionscritp classInspire dgt 網路技術分享_flash actionscritp class
Inspire dgt 網路技術分享_flash actionscritp classinspire digital
 
Blazor 與 Radzen 同行
Blazor 與 Radzen 同行Blazor 與 Radzen 同行
Blazor 與 Radzen 同行Jimmy Ho
 
ASP.Net MVC Framework
ASP.Net MVC FrameworkASP.Net MVC Framework
ASP.Net MVC Framework國昭 張
 
Study4.TW .NET Conf 2018 - Fp in c#
Study4.TW .NET Conf 2018  - Fp in c#Study4.TW .NET Conf 2018  - Fp in c#
Study4.TW .NET Conf 2018 - Fp in c#Chieh Kai Yang
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewControllerTom Fan
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kaoxwcoder
 
KISSY for starter
KISSY for starterKISSY for starter
KISSY for starteryiming he
 
Vue for beginners
Vue for beginnersVue for beginners
Vue for beginnersm_catt
 

Was ist angesagt? (14)

苦集滅道:透過開發客製 Sketch Plug-in 改善產品設計流程
苦集滅道:透過開發客製 Sketch Plug-in  改善產品設計流程苦集滅道:透過開發客製 Sketch Plug-in  改善產品設計流程
苦集滅道:透過開發客製 Sketch Plug-in 改善產品設計流程
 
React js
React jsReact js
React js
 
05 MapKit and Text Input
05 MapKit and Text Input05 MapKit and Text Input
05 MapKit and Text Input
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
 
Inspire dgt 網路技術分享_flash actionscritp class
Inspire dgt 網路技術分享_flash actionscritp classInspire dgt 網路技術分享_flash actionscritp class
Inspire dgt 網路技術分享_flash actionscritp class
 
Blazor 與 Radzen 同行
Blazor 與 Radzen 同行Blazor 與 Radzen 同行
Blazor 與 Radzen 同行
 
ASP.Net MVC Framework
ASP.Net MVC FrameworkASP.Net MVC Framework
ASP.Net MVC Framework
 
Study4.TW .NET Conf 2018 - Fp in c#
Study4.TW .NET Conf 2018  - Fp in c#Study4.TW .NET Conf 2018  - Fp in c#
Study4.TW .NET Conf 2018 - Fp in c#
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewController
 
AJAX Basic
AJAX BasicAJAX Basic
AJAX Basic
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kao
 
KISSY for starter
KISSY for starterKISSY for starter
KISSY for starter
 
HTML5 Samples
HTML5 SamplesHTML5 Samples
HTML5 Samples
 
Vue for beginners
Vue for beginnersVue for beginners
Vue for beginners
 

Ähnlich wie Retina mac

MobileWebAppFramework_V5_design
MobileWebAppFramework_V5_designMobileWebAppFramework_V5_design
MobileWebAppFramework_V5_designJackson Tian
 
Responsive Web UI Design
Responsive Web UI DesignResponsive Web UI Design
Responsive Web UI Designjay li
 
W3CTech美团react专场-Thinking in React
W3CTech美团react专场-Thinking in ReactW3CTech美团react专场-Thinking in React
W3CTech美团react专场-Thinking in React美团点评技术团队
 
SQL Server 資料庫版本控管
SQL Server 資料庫版本控管SQL Server 資料庫版本控管
SQL Server 資料庫版本控管Will Huang
 
twPolitwoops final
twPolitwoops finaltwPolitwoops final
twPolitwoops final橘毛 貓
 
John Resig Beijing 2010 (中文版)
John Resig Beijing 2010 (中文版)John Resig Beijing 2010 (中文版)
John Resig Beijing 2010 (中文版)Jia Mi
 
Html5移动网站开发实践
Html5移动网站开发实践Html5移动网站开发实践
Html5移动网站开发实践Web Zhao
 
Vulkan introduction
Vulkan introductionVulkan introduction
Vulkan introductionJiahan Su
 
06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollViewTom Fan
 
給 iOS 工程師的 Flutter 開發
給 iOS 工程師的 Flutter 開發給 iOS 工程師的 Flutter 開發
給 iOS 工程師的 Flutter 開發Weizhong Yang
 
不一樣的Web server... coServ
不一樣的Web server... coServ不一樣的Web server... coServ
不一樣的Web server... coServBen Lue
 
JSDC 2015 React-Native 實戰
JSDC 2015 React-Native 實戰JSDC 2015 React-Native 實戰
JSDC 2015 React-Native 實戰Sam Lee
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 
程式人雜誌 -- 2015 年1月號
程式人雜誌 -- 2015 年1月號程式人雜誌 -- 2015 年1月號
程式人雜誌 -- 2015 年1月號鍾誠 陳鍾誠
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介Allen Lsy
 
Lab01 cloud project
Lab01 cloud projectLab01 cloud project
Lab01 cloud projectJeff Chu
 
Eclipse開發平台快速入門
Eclipse開發平台快速入門Eclipse開發平台快速入門
Eclipse開發平台快速入門Luo Korth
 
Scala使用体验 玄畅 201512
Scala使用体验 玄畅 201512Scala使用体验 玄畅 201512
Scala使用体验 玄畅 201512Jiahua Zhu
 

Ähnlich wie Retina mac (20)

MobileWebAppFramework_V5_design
MobileWebAppFramework_V5_designMobileWebAppFramework_V5_design
MobileWebAppFramework_V5_design
 
I os 07
I os 07I os 07
I os 07
 
Responsive Web UI Design
Responsive Web UI DesignResponsive Web UI Design
Responsive Web UI Design
 
W3CTech美团react专场-Thinking in React
W3CTech美团react专场-Thinking in ReactW3CTech美团react专场-Thinking in React
W3CTech美团react专场-Thinking in React
 
SQL Server 資料庫版本控管
SQL Server 資料庫版本控管SQL Server 資料庫版本控管
SQL Server 資料庫版本控管
 
Vue
VueVue
Vue
 
twPolitwoops final
twPolitwoops finaltwPolitwoops final
twPolitwoops final
 
John Resig Beijing 2010 (中文版)
John Resig Beijing 2010 (中文版)John Resig Beijing 2010 (中文版)
John Resig Beijing 2010 (中文版)
 
Html5移动网站开发实践
Html5移动网站开发实践Html5移动网站开发实践
Html5移动网站开发实践
 
Vulkan introduction
Vulkan introductionVulkan introduction
Vulkan introduction
 
06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView06 Subclassing UIView and UIScrollView
06 Subclassing UIView and UIScrollView
 
給 iOS 工程師的 Flutter 開發
給 iOS 工程師的 Flutter 開發給 iOS 工程師的 Flutter 開發
給 iOS 工程師的 Flutter 開發
 
不一樣的Web server... coServ
不一樣的Web server... coServ不一樣的Web server... coServ
不一樣的Web server... coServ
 
JSDC 2015 React-Native 實戰
JSDC 2015 React-Native 實戰JSDC 2015 React-Native 實戰
JSDC 2015 React-Native 實戰
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
程式人雜誌 -- 2015 年1月號
程式人雜誌 -- 2015 年1月號程式人雜誌 -- 2015 年1月號
程式人雜誌 -- 2015 年1月號
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介
 
Lab01 cloud project
Lab01 cloud projectLab01 cloud project
Lab01 cloud project
 
Eclipse開發平台快速入門
Eclipse開發平台快速入門Eclipse開發平台快速入門
Eclipse開發平台快速入門
 
Scala使用体验 玄畅 201512
Scala使用体验 玄畅 201512Scala使用体验 玄畅 201512
Scala使用体验 玄畅 201512
 

Mehr von Weizhong Yang

怎樣在 Flutter app 中使用 Google Maps
怎樣在 Flutter app 中使用 Google Maps怎樣在 Flutter app 中使用 Google Maps
怎樣在 Flutter app 中使用 Google MapsWeizhong Yang
 
關於延長役期這件事情
關於延長役期這件事情關於延長役期這件事情
關於延長役期這件事情Weizhong Yang
 
導入 Flutter 前你應該知道的事
導入 Flutter 前你應該知道的事導入 Flutter 前你應該知道的事
導入 Flutter 前你應該知道的事Weizhong Yang
 
iPlayground: CarPlay and MFI Hearing Aids
iPlayground: CarPlay and MFI Hearing AidsiPlayground: CarPlay and MFI Hearing Aids
iPlayground: CarPlay and MFI Hearing AidsWeizhong Yang
 
CocoaPods private repo
CocoaPods private repoCocoaPods private repo
CocoaPods private repoWeizhong Yang
 
Flutter 踩雷心得
Flutter 踩雷心得Flutter 踩雷心得
Flutter 踩雷心得Weizhong Yang
 
那些年被蘋果 Ban 掉的 API
那些年被蘋果 Ban 掉的 API那些年被蘋果 Ban 掉的 API
那些年被蘋果 Ban 掉的 APIWeizhong Yang
 
給 iOS 工程師的 Vue.js 開發
給 iOS 工程師的 Vue.js 開發給 iOS 工程師的 Vue.js 開發
給 iOS 工程師的 Vue.js 開發Weizhong Yang
 
使用 switch/case 重構程式碼
使用 switch/case 重構程式碼使用 switch/case 重構程式碼
使用 switch/case 重構程式碼Weizhong Yang
 
怎樣寫出比較沒有問題的 Code
怎樣寫出比較沒有問題的 Code怎樣寫出比較沒有問題的 Code
怎樣寫出比較沒有問題的 CodeWeizhong Yang
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented ProgrammingWeizhong Yang
 
Mac OS X 與 iOS 的 Audio API
Mac OS X 與 iOS 的 Audio APIMac OS X 與 iOS 的 Audio API
Mac OS X 與 iOS 的 Audio APIWeizhong Yang
 
Python 的文件系統
Python 的文件系統Python 的文件系統
Python 的文件系統Weizhong Yang
 

Mehr von Weizhong Yang (19)

Flutter BLE
Flutter BLEFlutter BLE
Flutter BLE
 
怎樣在 Flutter app 中使用 Google Maps
怎樣在 Flutter app 中使用 Google Maps怎樣在 Flutter app 中使用 Google Maps
怎樣在 Flutter app 中使用 Google Maps
 
關於延長役期這件事情
關於延長役期這件事情關於延長役期這件事情
關於延長役期這件事情
 
Dart null safety
Dart null safetyDart null safety
Dart null safety
 
導入 Flutter 前你應該知道的事
導入 Flutter 前你應該知道的事導入 Flutter 前你應該知道的事
導入 Flutter 前你應該知道的事
 
Github Actions
Github ActionsGithub Actions
Github Actions
 
iPlayground: CarPlay and MFI Hearing Aids
iPlayground: CarPlay and MFI Hearing AidsiPlayground: CarPlay and MFI Hearing Aids
iPlayground: CarPlay and MFI Hearing Aids
 
CocoaPods private repo
CocoaPods private repoCocoaPods private repo
CocoaPods private repo
 
Flutter 踩雷心得
Flutter 踩雷心得Flutter 踩雷心得
Flutter 踩雷心得
 
那些年被蘋果 Ban 掉的 API
那些年被蘋果 Ban 掉的 API那些年被蘋果 Ban 掉的 API
那些年被蘋果 Ban 掉的 API
 
給 iOS 工程師的 Vue.js 開發
給 iOS 工程師的 Vue.js 開發給 iOS 工程師的 Vue.js 開發
給 iOS 工程師的 Vue.js 開發
 
使用 switch/case 重構程式碼
使用 switch/case 重構程式碼使用 switch/case 重構程式碼
使用 switch/case 重構程式碼
 
怎樣寫出比較沒有問題的 Code
怎樣寫出比較沒有問題的 Code怎樣寫出比較沒有問題的 Code
怎樣寫出比較沒有問題的 Code
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
Mac OS X 與 iOS 的 Audio API
Mac OS X 與 iOS 的 Audio APIMac OS X 與 iOS 的 Audio API
Mac OS X 與 iOS 的 Audio API
 
Python 的文件系統
Python 的文件系統Python 的文件系統
Python 的文件系統
 
Input Method Kit
Input Method KitInput Method Kit
Input Method Kit
 
Refactoring
RefactoringRefactoring
Refactoring
 
Core animation
Core animationCore animation
Core animation
 

Retina mac

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n