SlideShare a Scribd company logo
1 of 40
Download to read offline
Memory friendly UIScrollView
Yuichi Fujiki from ShopBack


28.04.2021
iOS DevScout Meetup


Apr 2021
A little about myself
• Senior iOS engineer at ShopBack


• In the
f
ield since 2011


• Hobby is to experiment with my body
2
Introduction
A little about ShopBack
3
Introduction
Agenda - Memory Friendly UIScrollView
• Image viewer


• Nested scroll view


• https://github.com/yfujiki/
iOSDevScoutMeetUpApr2021
4
Agenda
Image viewer
• What we want to build
5
Image Viewer
Simple version
• UIImageView inside UIScrollView


• Largest image for max zoom level
6
Image Viewer - Simple Version
Simple version
• Just let UIScrollView handle zoom
7
Image Viewer - Simple Version
Pitfall!
What is the issue?
• We are using too much memory
8
Image Viewer - Simple Version
• We will get pixellated view when zooming in
What if we just use small image?
9
Image Viewer - Simple Version
• Zoom level 1
Different images for different zoom levels
10
Image Viewer - CATiledLayer Version
• Zoom level 2
Different images for different zoom levels
11
Image Viewer - CATiledLayer Version
• Zoom level 4
Different images for different zoom levels
12
Image Viewer - CATiledLayer Version
CATiledLayer version
• CATiledLayer inside UIScrollView
13
Image Viewer - CATiledLayer Version
Ref: https://download.developer.apple.com/videos/wwdc_2010__sd/session_104__designing_apps_with_scroll_views.mov
Key concept about zoom levels
14
Image Viewer - CATiledLayer Version
UIScrollView
CATiledLayer
zoomScale = 1 zoomScale = 2 zoomScale = 4
levelOfDetail = 0 levelOfDetail = 1 levelOfDetail = 2
• levelsOfDetail = 3


• levelsOfDetailBias = 2
zoomScale = 1 is defined by


the original bounds of


the content view
Let’s say we set
f
irst bounds with 4 tiles…
15
Image Viewer - CATiledLayer Version
UIScrollView
CATiledLayer
zoomScale = 1 zoomScale = 2 zoomScale = 4
levelOfDetail = 0 levelOfDetail = 1 levelOfDetail = 2
zoomScale = 0.5 zoomScale = 1 zoomScale = 2
levelOfDetail = -1 levelOfDetail = 0 levelOfDetail = 1
• levelsOfDetail = 3


• levelsOfDetailBias = 1
Code - zoomScale = 1
Image Viewer - CATiledLayer Version
zoomScale = 1
Code - maximum zoom scale
Image Viewer - CATiledLayer Version
zoomScale = 4
Code - minimum zoom scale
Image Viewer - CATiledLayer Version
828x465
Code - levelsOfDetail
Image Viewer - CATiledLayer Version
Level 0
Level 1
Level 2
Code - drawRect
Image Viewer - CATiledLayer Version
Results
Image Viewer - CATiledLayer Version
UIImageView CATiledLayer
For precise measurement,


always use Instruments + real device
Nested scrollviews
• What we want to build


• UITableViews inside UIScrollView
22
Nested Scrollviews
Nested scrollviews
• What we want to build


• UITableViews inside UIScrollView
23
Nested Scrollviews
• Can’t we just use single UITableView


with multiple sections? - YES
Nested scrollviews
• In general, we could face complex layout requirements


that demands nested scrollview
24
Nested Scrollviews
• Can’t we use UICollectionViewCompositionalLayout?
• YES!!! But with limitations


• >= iOS 13


• Some complex layouts are still not possible
• Make UITableView frame to


be the same size as it’s content size


• Make UITableViews frames to
f
ill up


entire contentSize of UIScrollView


• UIScrollView (gray) will control all scrolling
Simple version
25
Nested Scrollviews - Simple Version
Implementation
26
Nested Scrollviews - Simple Version
Top Constraint
Vertical


Constraint
Bottom Constraint
• UITableView’s cell won’t reuse


• All cells for both UITableViews will be


loaded into memory
What is the issue?
27
Nested Scrollviews - Simple Version
• Outer ScrollView handles scroll when


both TableViews are visible


• Let the inner TableView handle scroll when


the TableView occupies the view
Switch Scrolling Control
28
Nested Scrollviews - Scrolling Switching Version
• Setting the content area properly
• Allow both gesture recognisers to work
• Switching scrolling control at the right timing
Implementation - setting content area
29
Nested Scrollviews - Scrolling Switching Version
Height


Constraint
Top Constraint
Bottom Constraint
Implementation - enable gesture
30
Nested Scrollviews - Scrolling Switching Version
• Without this, UITableView (inner) captures all gestures and UIScrollView
(outer) does not get any gesture at all
Implementation - switch scrolling control
31
Nested Scrollviews - Scrolling Switching Version
Results
32
Nested Scrollviews - Scrolling Switching Version
Simple Switch Scrolling
• Outer ScrollView handles scroll all the time


• As the viewport moves, calculate the frame


of each tableview to
f
it inside the viewport
Control the frames
33
Nested Scrollviews - Frame Controlling Version
Ref: https://oleb.net/blog/2014/05/scrollviews-inside-scrollviews/
Implementation
34
Nested Scrollviews - Frame Controlling Version
Implementation
35
Nested Scrollviews - Frame Controlling Version
Bounds


of


Scroll


View
Content


Area


of


Scroll


View
Content


Area


of


Table


View
Frame


of


Table


View
Implementation
36
Nested Scrollviews - Frame Controlling Version
Bounds


of


Scroll


View
Content


Area


of


Scroll


View
Content


Area


of


Table


View
Frame


of


Table


View
Implementation
37
Nested Scrollviews - Frame Controlling Version
Bounds


of


Scroll


View
Content


Area


of


Scroll


View
Content


Area


of


Table


View
Frame


of


Table


View
Implementation
38
Nested Scrollviews - Frame Controlling Version
Bounds


of


Scroll


View
Content


Area


of


Scroll


View
Content


Area


of


Table


View
Frame


of


Table


View
Results
39
Nested Scrollviews - Frame Controlling Version
Simple Frame Controlling
Conclusions
• Naive usage of UIScrollView leaves a lot of margin to
improve memory usage


• Keep experimenting. Even going through already
treaded path yourself will give you great insight


• https://github.com/yfujiki/iOSDevScoutMeetUpApr2021
40
Conclusion

More Related Content

Similar to Memory friendly UIScrollView

Bringing Swift into your Objective-C Projects
Bringing Swift into your Objective-C ProjectsBringing Swift into your Objective-C Projects
Bringing Swift into your Objective-C ProjectsRené Cacheaux
 
Synapse india reviews on i phone and android os
Synapse india reviews on i phone and android osSynapse india reviews on i phone and android os
Synapse india reviews on i phone and android ossaritasingh19866
 
TP_Webots_7mai2021.pdf
TP_Webots_7mai2021.pdfTP_Webots_7mai2021.pdf
TP_Webots_7mai2021.pdfkiiway01
 
Screen transitions with ease
Screen transitions with easeScreen transitions with ease
Screen transitions with easeOlivier Tassinari
 
Intro to UIKit • Made by Many
Intro to UIKit • Made by ManyIntro to UIKit • Made by Many
Intro to UIKit • Made by Manykenatmxm
 
LUMIA APP LABS #18: INTRODUCING NOKIA IMAGING SDK 1.0
LUMIA APP LABS #18: INTRODUCING NOKIA IMAGING SDK 1.0LUMIA APP LABS #18: INTRODUCING NOKIA IMAGING SDK 1.0
LUMIA APP LABS #18: INTRODUCING NOKIA IMAGING SDK 1.0Microsoft Mobile Developer
 
Сергій Міськів, «SwiftUI: Animations»
Сергій Міськів, «SwiftUI: Animations»Сергій Міськів, «SwiftUI: Animations»
Сергій Міськів, «SwiftUI: Animations»Sigma Software
 
OpenNTF Webinar, October 2020
OpenNTF Webinar, October 2020OpenNTF Webinar, October 2020
OpenNTF Webinar, October 2020Howard Greenberg
 
FrenchKit: SwiftUI Data Flow with Redux
FrenchKit: SwiftUI Data Flow with ReduxFrenchKit: SwiftUI Data Flow with Redux
FrenchKit: SwiftUI Data Flow with ReduxThomasRicouard
 
Head first iOS programming
Head first iOS programmingHead first iOS programming
Head first iOS programmingtedzhaoxa
 
Our Choice:电子书的新交互方式探讨
Our Choice:电子书的新交互方式探讨Our Choice:电子书的新交互方式探讨
Our Choice:电子书的新交互方式探讨foxgem
 
Apple Watch and WatchKit - A Technical Overview
Apple Watch and WatchKit - A Technical OverviewApple Watch and WatchKit - A Technical Overview
Apple Watch and WatchKit - A Technical OverviewSammy Sunny
 
Beginning iOS6 Development CH04 More User Interface Fun
Beginning iOS6 Development CH04 More User Interface FunBeginning iOS6 Development CH04 More User Interface Fun
Beginning iOS6 Development CH04 More User Interface FunAbdulrazzaq Alnajjar
 
Session 8 - Xcode 5 and interface builder for iOS 7 application
Session 8 - Xcode 5 and interface builder for iOS 7 applicationSession 8 - Xcode 5 and interface builder for iOS 7 application
Session 8 - Xcode 5 and interface builder for iOS 7 applicationVu Tran Lam
 
iOS UIStoryboard presentation
iOS UIStoryboard presentationiOS UIStoryboard presentation
iOS UIStoryboard presentationGerald Kim
 
Build Your First Android App Session #1
Build Your First Android App Session #1Build Your First Android App Session #1
Build Your First Android App Session #1Troy Miles
 

Similar to Memory friendly UIScrollView (20)

Bringing Swift into your Objective-C Projects
Bringing Swift into your Objective-C ProjectsBringing Swift into your Objective-C Projects
Bringing Swift into your Objective-C Projects
 
Synapse india reviews on i phone and android os
Synapse india reviews on i phone and android osSynapse india reviews on i phone and android os
Synapse india reviews on i phone and android os
 
Synapse india mobile apps update
Synapse india mobile apps updateSynapse india mobile apps update
Synapse india mobile apps update
 
Animation in iOS
Animation in iOSAnimation in iOS
Animation in iOS
 
TP_Webots_7mai2021.pdf
TP_Webots_7mai2021.pdfTP_Webots_7mai2021.pdf
TP_Webots_7mai2021.pdf
 
Screen transitions with ease
Screen transitions with easeScreen transitions with ease
Screen transitions with ease
 
Intro to UIKit • Made by Many
Intro to UIKit • Made by ManyIntro to UIKit • Made by Many
Intro to UIKit • Made by Many
 
LUMIA APP LABS #18: INTRODUCING NOKIA IMAGING SDK 1.0
LUMIA APP LABS #18: INTRODUCING NOKIA IMAGING SDK 1.0LUMIA APP LABS #18: INTRODUCING NOKIA IMAGING SDK 1.0
LUMIA APP LABS #18: INTRODUCING NOKIA IMAGING SDK 1.0
 
Сергій Міськів, «SwiftUI: Animations»
Сергій Міськів, «SwiftUI: Animations»Сергій Міськів, «SwiftUI: Animations»
Сергій Міськів, «SwiftUI: Animations»
 
OpenNTF Webinar, October 2020
OpenNTF Webinar, October 2020OpenNTF Webinar, October 2020
OpenNTF Webinar, October 2020
 
Introduction of Xcode
Introduction of XcodeIntroduction of Xcode
Introduction of Xcode
 
FrenchKit: SwiftUI Data Flow with Redux
FrenchKit: SwiftUI Data Flow with ReduxFrenchKit: SwiftUI Data Flow with Redux
FrenchKit: SwiftUI Data Flow with Redux
 
Head first iOS programming
Head first iOS programmingHead first iOS programming
Head first iOS programming
 
Our Choice:电子书的新交互方式探讨
Our Choice:电子书的新交互方式探讨Our Choice:电子书的新交互方式探讨
Our Choice:电子书的新交互方式探讨
 
Apple Watch and WatchKit - A Technical Overview
Apple Watch and WatchKit - A Technical OverviewApple Watch and WatchKit - A Technical Overview
Apple Watch and WatchKit - A Technical Overview
 
Beginning iOS6 Development CH04 More User Interface Fun
Beginning iOS6 Development CH04 More User Interface FunBeginning iOS6 Development CH04 More User Interface Fun
Beginning iOS6 Development CH04 More User Interface Fun
 
Session 8 - Xcode 5 and interface builder for iOS 7 application
Session 8 - Xcode 5 and interface builder for iOS 7 applicationSession 8 - Xcode 5 and interface builder for iOS 7 application
Session 8 - Xcode 5 and interface builder for iOS 7 application
 
Eclipse vs Visual Works
Eclipse vs Visual WorksEclipse vs Visual Works
Eclipse vs Visual Works
 
iOS UIStoryboard presentation
iOS UIStoryboard presentationiOS UIStoryboard presentation
iOS UIStoryboard presentation
 
Build Your First Android App Session #1
Build Your First Android App Session #1Build Your First Android App Session #1
Build Your First Android App Session #1
 

Recently uploaded

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 

Recently uploaded (20)

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 

Memory friendly UIScrollView

  • 1. Memory friendly UIScrollView Yuichi Fujiki from ShopBack 28.04.2021 iOS DevScout Meetup 
 Apr 2021
  • 2. A little about myself • Senior iOS engineer at ShopBack • In the f ield since 2011 • Hobby is to experiment with my body 2 Introduction
  • 3. A little about ShopBack 3 Introduction
  • 4. Agenda - Memory Friendly UIScrollView • Image viewer • Nested scroll view • https://github.com/yfujiki/ iOSDevScoutMeetUpApr2021 4 Agenda
  • 5. Image viewer • What we want to build 5 Image Viewer
  • 6. Simple version • UIImageView inside UIScrollView • Largest image for max zoom level 6 Image Viewer - Simple Version
  • 7. Simple version • Just let UIScrollView handle zoom 7 Image Viewer - Simple Version Pitfall!
  • 8. What is the issue? • We are using too much memory 8 Image Viewer - Simple Version
  • 9. • We will get pixellated view when zooming in What if we just use small image? 9 Image Viewer - Simple Version
  • 10. • Zoom level 1 Different images for different zoom levels 10 Image Viewer - CATiledLayer Version
  • 11. • Zoom level 2 Different images for different zoom levels 11 Image Viewer - CATiledLayer Version
  • 12. • Zoom level 4 Different images for different zoom levels 12 Image Viewer - CATiledLayer Version
  • 13. CATiledLayer version • CATiledLayer inside UIScrollView 13 Image Viewer - CATiledLayer Version Ref: https://download.developer.apple.com/videos/wwdc_2010__sd/session_104__designing_apps_with_scroll_views.mov
  • 14. Key concept about zoom levels 14 Image Viewer - CATiledLayer Version UIScrollView CATiledLayer zoomScale = 1 zoomScale = 2 zoomScale = 4 levelOfDetail = 0 levelOfDetail = 1 levelOfDetail = 2 • levelsOfDetail = 3 • levelsOfDetailBias = 2 zoomScale = 1 is defined by 
 the original bounds of 
 the content view
  • 15. Let’s say we set f irst bounds with 4 tiles… 15 Image Viewer - CATiledLayer Version UIScrollView CATiledLayer zoomScale = 1 zoomScale = 2 zoomScale = 4 levelOfDetail = 0 levelOfDetail = 1 levelOfDetail = 2 zoomScale = 0.5 zoomScale = 1 zoomScale = 2 levelOfDetail = -1 levelOfDetail = 0 levelOfDetail = 1 • levelsOfDetail = 3 • levelsOfDetailBias = 1
  • 16. Code - zoomScale = 1 Image Viewer - CATiledLayer Version zoomScale = 1
  • 17. Code - maximum zoom scale Image Viewer - CATiledLayer Version zoomScale = 4
  • 18. Code - minimum zoom scale Image Viewer - CATiledLayer Version 828x465
  • 19. Code - levelsOfDetail Image Viewer - CATiledLayer Version Level 0 Level 1 Level 2
  • 20. Code - drawRect Image Viewer - CATiledLayer Version
  • 21. Results Image Viewer - CATiledLayer Version UIImageView CATiledLayer For precise measurement, 
 always use Instruments + real device
  • 22. Nested scrollviews • What we want to build • UITableViews inside UIScrollView 22 Nested Scrollviews
  • 23. Nested scrollviews • What we want to build • UITableViews inside UIScrollView 23 Nested Scrollviews • Can’t we just use single UITableView 
 with multiple sections? - YES
  • 24. Nested scrollviews • In general, we could face complex layout requirements 
 that demands nested scrollview 24 Nested Scrollviews • Can’t we use UICollectionViewCompositionalLayout? • YES!!! But with limitations • >= iOS 13 • Some complex layouts are still not possible
  • 25. • Make UITableView frame to 
 be the same size as it’s content size • Make UITableViews frames to f ill up 
 entire contentSize of UIScrollView • UIScrollView (gray) will control all scrolling Simple version 25 Nested Scrollviews - Simple Version
  • 26. Implementation 26 Nested Scrollviews - Simple Version Top Constraint Vertical 
 Constraint Bottom Constraint
  • 27. • UITableView’s cell won’t reuse • All cells for both UITableViews will be 
 loaded into memory What is the issue? 27 Nested Scrollviews - Simple Version
  • 28. • Outer ScrollView handles scroll when 
 both TableViews are visible • Let the inner TableView handle scroll when 
 the TableView occupies the view Switch Scrolling Control 28 Nested Scrollviews - Scrolling Switching Version • Setting the content area properly • Allow both gesture recognisers to work • Switching scrolling control at the right timing
  • 29. Implementation - setting content area 29 Nested Scrollviews - Scrolling Switching Version Height Constraint Top Constraint Bottom Constraint
  • 30. Implementation - enable gesture 30 Nested Scrollviews - Scrolling Switching Version • Without this, UITableView (inner) captures all gestures and UIScrollView (outer) does not get any gesture at all
  • 31. Implementation - switch scrolling control 31 Nested Scrollviews - Scrolling Switching Version
  • 32. Results 32 Nested Scrollviews - Scrolling Switching Version Simple Switch Scrolling
  • 33. • Outer ScrollView handles scroll all the time • As the viewport moves, calculate the frame 
 of each tableview to f it inside the viewport Control the frames 33 Nested Scrollviews - Frame Controlling Version Ref: https://oleb.net/blog/2014/05/scrollviews-inside-scrollviews/
  • 34. Implementation 34 Nested Scrollviews - Frame Controlling Version
  • 35. Implementation 35 Nested Scrollviews - Frame Controlling Version Bounds of 
 Scroll 
 View Content 
 Area of 
 Scroll 
 View Content 
 Area of 
 Table 
 View Frame of 
 Table 
 View
  • 36. Implementation 36 Nested Scrollviews - Frame Controlling Version Bounds of 
 Scroll 
 View Content 
 Area of 
 Scroll 
 View Content 
 Area of 
 Table 
 View Frame of 
 Table 
 View
  • 37. Implementation 37 Nested Scrollviews - Frame Controlling Version Bounds of 
 Scroll 
 View Content 
 Area of 
 Scroll 
 View Content 
 Area of 
 Table 
 View Frame of 
 Table 
 View
  • 38. Implementation 38 Nested Scrollviews - Frame Controlling Version Bounds of 
 Scroll 
 View Content 
 Area of 
 Scroll 
 View Content 
 Area of 
 Table 
 View Frame of 
 Table 
 View
  • 39. Results 39 Nested Scrollviews - Frame Controlling Version Simple Frame Controlling
  • 40. Conclusions • Naive usage of UIScrollView leaves a lot of margin to improve memory usage • Keep experimenting. Even going through already treaded path yourself will give you great insight • https://github.com/yfujiki/iOSDevScoutMeetUpApr2021 40 Conclusion