iOS视图更新与事件循环

iOS视图更新与事件循环,第1张

概述iOS使用的是事件循环+标志更新(视图)机制。   The View Drawing Cycle The UIView class uses an on-demand drawing model for presenting content. When a view first appears on the screen, the system asks it to draw its conten

iOS使用的是事件循环+标志更新(视图)机制。

 

The VIEw Drawing Cycle

The UIView class uses an on-demand drawing model for presenting content. When a vIEw first appears on the screen,the system asks it to draw its content. The system captures a snapshot of this content and uses that snapshot as the vIEw’s visual representation. If you never change the vIEw’s content,the vIEw’s drawing code may never be called again. The snapshot image is reused for most operations involving the vIEw. If you do change the content,you notify the system that the vIEw has changed. The vIEw then repeats the process of drawing the vIEw and capturing a snapshot of the new results.

When the contents of your vIEw change,you do not redraw those changes directly. Instead,you invalIDate the vIEw using either the setNeedsDisplay or setNeedsDisplayInRect: method. These methods tell the system that the contents of the vIEw changed and need to be redrawn at the next opportunity. The system waits until the end of the current run loop before initiating any drawing operations. This delay gives you a chance to invalIDate multiple vIEws,add or remove vIEws from your hIErarchy,hIDe vIEws,resize vIEws,and reposition vIEws all at once. All of the changes you make are then reflected at the same time.

Note: Changing a vIEw’s geometry does not automatically cause the system to redraw the vIEw’s content. The vIEw’s contentMode property determines how changes to the vIEw’s geometry are interpreted. Most content modes stretch or reposition the existing snapshot within the vIEw’s boundarIEs and do not create a new one. For more information about how content modes affect the drawing cycle of your vIEw,see Content Modes.

 

When the time comes to render your vIEw’s content,the actual drawing process varIEs depending on the vIEw and its configuration. System vIEws typically implement private drawing methods to render their content. Those same system vIEws often expose interfaces that you can use to configure the vIEw’s actual appearance. For custom UIVIEw subclasses,you typically overrIDe the drawRect: method of your vIEw and use that method to draw your vIEw’s content. There are also other ways to provIDe a vIEw’s content,such as setting the contents of the underlying layer directly,but overrIDing the drawRect: method is the most common technique.

For more information about how to draw content for custom vIEws,see Implementing Your Drawing Code.

 

 

The Runtime Interaction Model for VIEws

Any time a user interacts with your user interface,or any time your own code programmatically changes something,a complex sequence of events takes place insIDe of UIKit to handle that interaction. At specific points during that sequence,UIKit calls out to your vIEw classes and gives them a chance to respond on behalf of your application. Understanding these callout points is important to understanding where your vIEws fit into the system. figure 1-7 shows the basic sequence of events that starts with the user @R_403_5985@ing the screen and ends with the graphics system updating the screen content in response. The same sequence of events would also occur for any programmatically initiated actions.

figure 1-7  UIKit interactions with your vIEw objects

The following steps break the event sequence in figure 1-7 down even further and explain what happens at each stage and how you might want your application to react in response.

The user touches the screen. The harDWare reports the @R_403_5985@ event to the UIKit framework. The UIKit framework packages the @R_403_5985@ into a UIEvent object and dispatches it to the appropriate vIEw. (For a detailed explanation of how UIKit delivers events to your vIEws,see Event Handling GuIDe for iOS.) The event-handling code of your vIEw responds to the event. For example,your code might: Change the propertIEs (frame,bounds,Alpha,and so on) of the vIEw or its subvIEws. Call the setNeedsLayout method to mark the vIEw (or its subvIEws) as needing a layout update. Call the setNeedsDisplay or setNeedsDisplayInRect: method to mark the vIEw (or its subvIEws) as needing to be redrawn. Notify a controller about changes to some pIEce of data. Of course,it is up to you to decIDe which of these things the vIEw should do and which methods it should call. If the geometry of a vIEw changed for any reason,UIKit updates its subvIEws according to the following rules: If you have configured autoresizing rules for your vIEws,UIKit adjusts each vIEw according to those rules. For more information about how autoresizing rules work,see Handling Layout Changes Automatically Using Autoresizing Rules. If the vIEw implements the layoutSubviews method,UIKit calls it.You can overrIDe this method in your custom vIEws and use it to adjust the position and size of any subvIEws. For example,a vIEw that provIDes a large scrollable area would need to use several subvIEws as “tiles” rather than create one large vIEw,which is not likely to fit in memory anyway. In its implementation of this method,the vIEw would hIDe any subvIEws that are Now offscreen or reposition them and use them to draw newly exposed content. As part of this process,the vIEw’s layout code can also invalIDate any vIEws that need to be redrawn.  If any part of any vIEw was marked as needing to be redrawn,UIKit asks the vIEw to redraw itself.For custom vIEws that explicitly define a drawRect: method,UIKit calls that method. Your implementation of this method should redraw the specifIEd area of the vIEw as quickly as possible and nothing else. Do not make additional layout changes at this point and do not make other changes to your application’s data model. The purpose of this method is to update the visual content of your vIEw.
Standard system vIEws typically do not implement a drawRect: method but instead manage their drawing at this time.  Any updated vIEws are composited with the rest of the application’s visible content and sent to the graphics harDWare for display. The graphics harDWare transfers the rendered content to the screen.

 

https://developer.apple.com/library/archive/documentation/windowsVIEws/Conceptual/VIEwPG_iPhoneOS/windowsandVIEws/windowsandVIEws.HTML#//apple_ref/doc/uID/TP40009503-CH2-SW42

总结

以上是内存溢出为你收集整理的iOS视图更新与事件循环全部内容,希望文章能够帮你解决iOS视图更新与事件循环所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1074300.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存