My Github address
Notes on data Structures and Algorithms
Notes for geek Time iOS Developer Class
IOS large factory interview high frequency algorithm summary
Summary of iOS interview materials
UITableView related
I. Reuse mechanism
1. How does iOS implement cell reuse mechanism?
A1-A7
Use the sameidentifer
whentableView
Slide up,A1
Once the page is underlined, it is placed in the reuse pool.- when
A7
When you are about to show it, you will first see the same in the reuse poolidentifer
thecell
It can be reused, if it exists, it can be used directly, if it does not exist, it can create a new onecell
.
2. How to implement the reuse mechanism manually?
ViewReusePool
Declaration of a class
ViewReusePool
The realization of the class
dequeueReusableView
function
AddUsingView:
function
reset
function
ViewReusePool
The use of the class
2. Data source synchronization
- When the data source is in
The main thread
There aredelete
Operation at the same timeThe child thread
And on theTo load more
When the data is operated onData source synchronization problems
.
1. Data source synchronization solution
A. Concurrent access and data copy
The child thread
returnThe main thread
Is still containing the deleted data.- in
The main thread
Record the deletion operation. After theThe child thread
During data synchronization, the deletion operation was synchronized.
B. Serial access
- will
The child thread
Data synchronization andThe main thread
All delete operations in oneSerial queues
In the execution. - There may be a delay in deleting.
Event passing & view corresponding
UIView and CALayer
1. The relationship and difference between UIView and CALayer?
A, relationship,
UIView
The object of thelayer
Points to aCALayer
variableUIView
The object of thebackgroundColor
Properties, rightCALayer
Encapsulation of a property with the same name.UIView
The presentation section is made byCALayer
In thecontents
To decide.contents
The correspondingbacking store
It’s actually abitmap
The bitmap.
B, the difference between
UIView
Provide content for it, as well as handle events like touches and participate in the response chain.CALayer
Responsible for displaying contentcontents
.
2. Why UIView is responsible for touch events and CALayer for display?
- Design patterns,
Single responsibility
The principle.
Event passing and view response chain
1. When clicking on the View C2 area, how does the system find the response View?
A. The process of event transmission
- When the user clicks on the screen, the event will be
UIApplication
Accept and pass toUIWindow
. UIWindow
callhitTest
Function,hitTest
In the callpointInside
Determines whether the event is in the view.- if
false
, the view is returned, and the event passing process ends. - if
true
, you canFlashback traversal
The view ofChild views
And callChild views
thehitTest
Function. - Find the final
hitTest
fortrue
theChild views
And return in turn, the event passing process ends.
B,hitTest
Internal system implementation
- Called in the current view subview
hitTest
Function, the current coordinate needs to be converted toChild views
Coordinates in.
2. How to make only the circular area of the square image receive event response?
- Override view
pointInside
Function that causes the click region to return within a circletrue
Otherwise returnfalse
.
3. View response process
A, the event response is passResponse chain
To pass it on.
UIView
Through inheritanceUIResponder
, has the following functions
B. Who will respond to the event after it is delivered?
- If the response view cannot handle the response event, the response event passes
Response chain
Passed to theParent view
Try processing until passed toUIApplication
. If passing to UIApplication still does not process the response event, the event is ignored.
Principle of image display
First, image display process
CPU
andGPU
Is through theEvent bus
Linked together.CPU
The output of theThe bitmap
At the appropriate timeEvent bus
toGPU
.GPU
toThe bitmap
forApply colours to a drawing
, and then put the result into the frame buffer.Video controller
throughVsync signal
, at the specified time (16.7 ms
) before, fromThe frame buffer
Extract the screen display content from, and then display it on the monitor.
Ii. UI view display process
- When creating a
UIView
Object whose display portion is represented byCALayer
To control. CALayer
There is acontents
Properties, that’s what’s ultimately drawn on the screenThe bitmap
.- In the drawing
contents
Content, the system will call backdrawRect:
Function, we can add draw content to the function. - Draw good
The bitmap
throughCore Animation
Frame, ultimately viaGPU
In the middle ofOpenGL
Render pipeline, render on screen.
Three, CPU working process
1, the Layout
- The UI layout (
frame
Settings) - Text computing (
size
Computing)
2, the Display
- To draw (
drawRect:
)
3, Prepare
- Picture codec
4, Commit
- Submit the bitmap
Iv. GPU rendering pipeline process
Stenton-drop frame causes
- According to the second
60FPS
Refresh rate, every other16.7 ms
There will be oneVsync
The signal. - in
16.7 ms
The need toCPU
andGPU
Synergy produces this frame of picture and next timeVsync
When the signal comes, display this frame. - if
CPU
andGPU
Working hours exceeded16.7 ms
, then whenVsync
When the signal comes, a picture that cannot provide this frame will appearFrame drop
Phenomenon. - The screen not shown in the previous frame will be displayed in the next one
Vsync
Display as the signal comes in.
1. Sliding optimization scheme
1, CPU,
- Object creation,
Adjust the
,The destruction
You can put it on a child thread. - Pre-typesetting (layout calculation, text calculation) operations can be placed in child thread operations.
- Prerender (text, etc
Asynchronous rendering
,Picture codec
Etc.) to reduce CPU time.
2, the GPU
- avoid
Off-screen rendering
To reduce texture rendering time. - if
View hierarchy complexity
, the GPU does a lot of computation during view composition. Can be achieved byAsynchronous rendering
And other mechanisms to reduce view hierarchy and reduce GPU pressure.
Drawing principle & asynchronous drawing
First, UIView rendering principle
- When calling
setNeedsDispaly
Function, which is actually calledview.layer
thesetNeedsDispaly
Function. - This function will take
layer
Mark,runloop
At the end of the callCALayer display
Function to enter the actual drawing of the current view. - in
CALayer display
Function to determine whether its proxy is respondingDisplayLayer:
Function, ifYES
, can be carried outAsynchronous rendering
Otherwise enterSystem drawing process
.
Second, the drawing process of the system
layer
Will create abacking store
In thedrawRect:
The context is available in the function.layer
Will determine whether there is a proxy, if there is a proxy, after the system internal drawing is complete, will callUIView drawRect:
Allows you to add changes based on system drawing.- In the end by the
CALayer
uploadbacking store
toGPU
.
Three, asynchronous drawing
- if
layer
If an agent exists, it is executed by the agentdisplay:
The bitmap function generates the bitmap and sets thebitmap
As alayer.contents
Properties.
Off-screen rendering
On-screen Rendering (ON-screen Rendering)
Refers to the current screen rendering, referring toGPU
The render operation is performed in the buffer currently used to display the screen.Off-screen Rendering (OFF-screen Rendering)
It means off-screen rendering. It meansGPU
Outside of the current screen bufferA new
A buffer for rendering operations.
What scene triggers off-screen rendering?
- Rounded corners (needs to be used with maskToBounds)
- Layer mask
- shadow
- rasterizer
Second, why to avoid off-screen rendering?
- Create a new render
The buffer
There is an overhead in memory. - Multi-channel rendering pipelines, which will eventually need to be synthesized, will be involved
Context switch
, an increase ofGPU
Overhead. - Summary: Off-screen rendering will increase
GPU
The processing time that this may lead toCPU
+GPU
The total processing time exceeds16.7 ms
, thus emergeFrame drop caton
The phenomenon.
UI View interview summary
- What is the UI event passing mechanism of the system?
- inspection
hitTest
andpointInside
Internal implementation.
- inspection
- What are some solutions or ideas for making UITableView scroll more smoothly?
The CPU has
In theThe child thread creates, adjusts, destroys, prearranges, and asynchronously draws images
.
- What is off-screen rendering?
- Create a new buffer for rendering outside the current screen buffer.
- What’s the relationship between UIView and CALayer?
UIView
To be responsible for theevents
andIncident response
.CALayer
Responsible for the UIView shows
.- Use the six design principles of design patterns
Single responsibility
The principle.