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-A7Use the sameidentiferwhentableViewSlide up,A1Once the page is underlined, it is placed in the reuse pool.
  • whenA7When you are about to show it, you will first see the same in the reuse poolidentiferthecellIt 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?

  • ViewReusePoolDeclaration of a class

  • ViewReusePoolThe realization of the class

  • dequeueReusableViewfunction

  • AddUsingView:function

  • resetfunction

  • ViewReusePoolThe use of the class

2. Data source synchronization

  • When the data source is inThe main threadThere aredeleteOperation at the same timeThe child threadAnd on theTo load moreWhen the data is operated onData source synchronization problems.

1. Data source synchronization solution

A. Concurrent access and data copy
  • The child threadreturnThe main threadIs still containing the deleted data.
  • inThe main threadRecord the deletion operation. After theThe child threadDuring data synchronization, the deletion operation was synchronized.

B. Serial access
  • willThe child threadData synchronization andThe main threadAll delete operations in oneSerial queuesIn 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,

  • UIViewThe object of thelayerPoints to aCALayervariable
  • UIViewThe object of thebackgroundColorProperties, rightCALayerEncapsulation of a property with the same name.
  • UIViewThe presentation section is made byCALayerIn thecontentsTo decide.contentsThe correspondingbacking storeIt’s actually abitmapThe bitmap.
B, the difference between
  • UIViewProvide content for it, as well as handle events like touches and participate in the response chain.
  • CALayerResponsible for displaying contentcontents.

2. Why UIView is responsible for touch events and CALayer for display?

  • Design patterns,Single responsibilityThe 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 beUIApplicationAccept and pass toUIWindow.
  • UIWindowcallhitTestFunction,hitTestIn the callpointInsideDetermines whether the event is in the view.
  • iffalse, the view is returned, and the event passing process ends.
  • iftrue, you canFlashback traversalThe view ofChild viewsAnd callChild viewsthehitTestFunction.
  • Find the finalhitTestfortruetheChild viewsAnd return in turn, the event passing process ends.
B,hitTestInternal system implementation

  • Called in the current view subviewhitTestFunction, the current coordinate needs to be converted toChild viewsCoordinates in.

2. How to make only the circular area of the square image receive event response?

  • Override viewpointInsideFunction that causes the click region to return within a circletrueOtherwise returnfalse.

3. View response process

A, the event response is passResponse chainTo pass it on.

  • UIViewThrough 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 passesResponse chainPassed to theParent viewTry 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

  • CPUandGPUIs through theEvent busLinked together.
  • CPUThe output of theThe bitmapAt the appropriate timeEvent bustoGPU.
  • GPUtoThe bitmapforApply colours to a drawing, and then put the result into the frame buffer.
  • Video controllerthroughVsync signal, at the specified time (16.7 ms) before, fromThe frame bufferExtract the screen display content from, and then display it on the monitor.

Ii. UI view display process

  • When creating aUIViewObject whose display portion is represented byCALayerTo control.
  • CALayerThere is acontentsProperties, that’s what’s ultimately drawn on the screenThe bitmap.
  • In the drawingcontentsContent, the system will call backdrawRect:Function, we can add draw content to the function.
  • Draw goodThe bitmapthroughCore AnimationFrame, ultimately viaGPUIn the middle ofOpenGLRender pipeline, render on screen.

Three, CPU working process

1, the Layout

  • The UI layout (frameSettings)
  • Text computing (sizeComputing)

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 second60FPSRefresh rate, every other16.7 msThere will be oneVsyncThe signal.
  • in16.7 msThe need toCPUandGPUSynergy produces this frame of picture and next timeVsyncWhen the signal comes, display this frame.
  • ifCPUandGPUWorking hours exceeded16.7 ms, then whenVsyncWhen the signal comes, a picture that cannot provide this frame will appearFrame dropPhenomenon.
  • The screen not shown in the previous frame will be displayed in the next oneVsyncDisplay as the signal comes in.

1. Sliding optimization scheme

1, CPU,

  • Object creation,Adjust the,The destructionYou can put it on a child thread.
  • Pre-typesetting (layout calculation, text calculation) operations can be placed in child thread operations.
  • Prerender (text, etcAsynchronous rendering,Picture codecEtc.) to reduce CPU time.

2, the GPU

  • avoidOff-screen renderingTo reduce texture rendering time.
  • ifView hierarchy complexity, the GPU does a lot of computation during view composition. Can be achieved byAsynchronous renderingAnd other mechanisms to reduce view hierarchy and reduce GPU pressure.

Drawing principle & asynchronous drawing

First, UIView rendering principle

  • When callingsetNeedsDispalyFunction, which is actually calledview.layerthesetNeedsDispalyFunction.
  • This function will takelayerMark,runloopAt the end of the callCALayer displayFunction to enter the actual drawing of the current view.
  • inCALayer displayFunction to determine whether its proxy is respondingDisplayLayer:Function, ifYES, can be carried outAsynchronous renderingOtherwise enterSystem drawing process.

Second, the drawing process of the system

  • layerWill create abacking storeIn thedrawRect:The context is available in the function.
  • layerWill 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 theCALayeruploadbacking storetoGPU.

Three, asynchronous drawing

  • iflayerIf an agent exists, it is executed by the agentdisplay:The bitmap function generates the bitmap and sets thebitmapAs alayer.contentsProperties.

Off-screen rendering

  • On-screen Rendering (ON-screen Rendering)Refers to the current screen rendering, referring toGPUThe 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 meansGPUOutside of the current screen bufferA newA 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 renderThe bufferThere is an overhead in memory.
  • Multi-channel rendering pipelines, which will eventually need to be synthesized, will be involvedContext switch, an increase ofGPUOverhead.
  • Summary: Off-screen rendering will increaseGPUThe processing time that this may lead toCPU + GPUThe total processing time exceeds16.7 ms, thus emergeFrame drop catonThe phenomenon.

UI View interview summary

  • What is the UI event passing mechanism of the system?
    • inspectionhitTestandpointInsideInternal implementation.
  • What are some solutions or ideas for making UITableView scroll more smoothly?
    • The CPU hasIn 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?
    • UIViewTo be responsible for theeventsandIncident response.
    • CALayerResponsible for the UIView shows.
    • Use the six design principles of design patternsSingle responsibilityThe principle.