UIView 的粗浅解析

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content. The UIView class itself provides basic behavior for filling its rectangular area with a background color. More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself. The UIKit framework also includes a set of standard subclasses that range from simple buttons to complex tables and can be used as-is. For example, a UILabelobject draws a text string and a UIImageView object draws an image.

Because view objects are the main way your application interacts with the user, they have a number of responsibilities. Here are just a few:

  • Drawing and animation

    • Views draw content in their rectangular area using technologies such as UIKit, Core Graphics, and OpenGL ES.
    • Some view properties can be animated to new values.
  • Layout and subview management
    • A view may contain zero or more subviews.
    • Each view defines its own default resizing behavior in relation to its parent view.
    • A view can define the size and position of its subviews as needed.
  • Event handling
    • A view is a responder and can handle touch events and other events defined by the UIResponder class.
    • Views can use the addGestureRecognizer: method to install gesture recognizers to handle common gestures.

Views can embed other views and create sophisticated visual hierarchies. This creates a parent-child relationship between the view being embedded (known as the subview) and the parent view doing the embedding (known as the superview). Normally, a subview’s visible area is not clipped to the bounds of its superview, but in iOS you can use the clipsToBounds property to alter that behavior. A parent view may contain any number of subviews but each subview has only one superview, which is responsible for positioning its subviews appropriately.

The geometry of a view is defined by its framebounds, and center properties. The frame defines the origin and dimensions of the view in the coordinate system of its superview and is commonly used during layout to adjust the size or position of the view. The center property can be used to adjust the position of the view without changing its size. The bounds defines the internal dimensions of the view as it sees them and is used almost exclusively in custom drawing code. The size portion of the frame and bounds rectangles are coupled together so that changing the size of either rectangle updates the size of both.

For detailed information about how to use the UIView class, see View Programming Guide for iOS.

NOTE

In iOS 2.x, the maximum size of a UIView object is 1024 x 1024 points. In iOS 3.0 and later, views are no longer restricted to this maximum size but are still limited by the amount of memory they consume. It is in your best interests to keep view sizes as small as possible. Regardless of which version of iOS is running, you should consider tiling any content that is significantly larger than the dimensions the screen.

Creating a View

To create a view programmatically, you can use code like the following:

  1. CGRect viewRect = CGRectMake(10, 10, 100, 100); 
  2. UIView* myView = [[UIView alloc] initWithFrame:viewRect]; 

This code creates the view and positions it at the point (10, 10) in its superview’s coordinate system (once it is added to that superview). To add a subview to another view, you use the addSubview: method. In iOS, sibling views may overlap each other without any issues, allowing complex view placement. The addSubview: method places the specified view on top of other siblings. You can specify the relative z-order of a subview by adding it using the insertSubview:aboveSubview: and insertSubview:belowSubview: methods. You can also exchange the position of already added subviews using the exchangeSubviewAtIndex:withSubviewAtIndex:method.

When creating a view, it is important to assign an appropriate value to the autoresizingMask property to ensure the view resizes correctly. View resizing primarily occurs when the orientation of your application’s interface changes but it may happen at other times as well. For example, calling the setNeedsLayout method forces your view to update its layout.

The View Drawing Cycle

View drawing occurs on an as-needed basis. When a view is first shown, or when all or part of it becomes visible due to layout changes, the system asks the view to draw its contents. For views that contain custom content using UIKit or Core Graphics, the system calls the view’s drawRect: method. Your implementation of this method is responsible for drawing the view’s content into the current graphics context, which is set up by the system automatically prior to calling this method. This creates a static visual representation of your view’s content that can then be displayed on the screen.

When the actual content of your view changes, it is your responsibility to notify the system that your view needs to be redrawn. You do this by calling your view’s setNeedsDisplay or setNeedsDisplayInRect: method of the view. These methods let the system know that it should update the view during the next drawing cycle. Because it waits until the next drawing cycle to update the view, you can call these methods on multiple views to update them at the same time.

NOTE

If you are using OpenGL ES to do your drawing, you should use the GLKView class instead of subclassing UIView. For more information about how to draw using OpenGL ES, see OpenGL ES Programming Guide for iOS.

For detailed information about the view drawing cycle and the role your views have in this cycle, see View Programming Guide for iOS.

Animations

Changes to several view properties can be animated—that is, changing the property creates an animation that conveys the change to the user over a short period of time. The UIView class does most of the work of performing the actual animations but you must still indicate which property changes you want to be animated. There are two different ways to initiate animations:

  • In iOS 4 and later, use the block-based animation methods. (Recommended)
  • Use the begin/commit animation methods.

The block-based animation methods (such as animateWithDuration:animations:) greatly simplify the creation of animations. With one method call, you specify the animations to be performed and the options for the animation. However, block-based animations are available only in iOS 4 and later. If your application runs on earlier versions of iOS, you must use the beginAnimations:context: and commitAnimations class methods to mark the beginning and ending of your animations.

The following properties of the UIView class are animatable:

For more information about how to configure animations, see View Programming Guide for iOS.

抛砖引玉:

1)继承自UIresponder,因此有touch和手势的方法,直接重写就可以实现在当前View进行对具体的操作的处理。

  • A view is a responder and can handle touch events and other events defined by the UIResponder class.
  • Views can use the addGestureRecognizer: method to install gesture recognizers to handle common gestures.

2)具有动画能力。The block-based animation methods (such as animateWithDuration:animations:);使用块可以快速实现关键帧动画、基础动画。

3)具有绘制自己的方法drawRect:系统会优先调用此方法在当前绘图文本上绘制, This creates a static visual representation of your view’s content that can then be displayed on the screen,是静态可视的。因此当你在系统绘制后改变视图的内容,需要自己手动调用When the actual content of your view changes, it is your responsibility to notify the system that your view needs to be redrawn. You do this by calling your view’s setNeedsDisplay or setNeedsDisplayInRect: method of the view,来强制让系统在下一个绘图周期更新视图内容,这样视图才会按照你想的那样更新。

4)对于绘制会使用 UIKit or Core Graphics,知道这两者的区别和联系,以及具体的怎么使用,这样就可以自定义视图的绘制内容。

5)理解UIView的几个属性,center,frame,bounds,重要的是理解他们相对于那个视图。

时间: 2024-10-22 01:36:18

UIView 的粗浅解析的相关文章

时序数据库连载系列: 时序数据库一哥InfluxDB之存储机制解析

InfluxDB 的存储机制解析 本文介绍了InfluxDB对于时序数据的存储/索引的设计.由于InfluxDB的集群版已在0.12版就不再开源,因此如无特殊说明,本文的介绍对象都是指 InfluxDB 单机版 1. InfluxDB 的存储引擎演进 尽管InfluxDB自发布以来历时三年多,其存储引擎的技术架构已经做过几次重大的改动, 以下将简要介绍一下InfluxDB的存储引擎演进的过程. 1.1 演进简史 版本0.9.0之前 **基于 LevelDB的LSMTree方案** 版本0.9.0

程序员的十层楼

第1层 菜鸟 第1层楼属于地板层,迈进这层楼的门槛是很低的.基本上懂计算机的基本操作,了解计算机专业的一些基础知识,掌握一门基本的编程语言如C/C++,或者Java,或者JavaScript,...,均可入门迈进这层. 在这层上,中国有着绝对的优势,除了从计算机专业毕业的众多人数外,还有大量的通信.自动化.数学等相关专业的人士进入这一行,此外还有众多的其他专业转行的人士,人数绝对比西方多出甚多.并且还有一个优势就是我们这层人员的平均智商比西方肯定高. 没有多少人愿意一辈子做菜鸟,因为做"菜鸟&q

程序员的十个层次,你属于哪一层?

自西方文艺复兴以来,中国在自然科学方面落后西方很多,软件领域也不例外.当然现在中国的许多程序员们对此可能有许多不同的意见,有些人认为中国的程序员水平远落后于西方,有些则认为中国的程序员个人能力并不比西方的程序员差,只是整个软件产业落后而已. 那么,到底中国的程序员水平比西方程序员水平差,还是中国有许多优秀的程序员达到或超过了西方程序员同等水平呢?要解决这个问题,必须先知道程序员有多少种技术层级,每个层级需要什么样的技术水平,然后再比较中国和西方在各个技术层级的人数,就可以知道到底有没有差距,差距

程序员的十个层次(转)

自西方文艺复兴以来,中国在自然科学方面落后西方很多,软件领域也不例外.当然现在中国的许多程序员们对此可能有许多不同的意见,有些人认为中国的程序员水平远落后于西方,有些则认为中国的程序员个人能力并不比西方的程序员差,只是整个软件产业落后而已. 那么,到底中国的程序员水平比西方程序员水平差,还是中国有许多优秀的程序员达到或超过了西方程序员同等水平呢?要解决这个问题,必须先知道程序员有多少种技术层级,每个层级需要什么样的技术水平,然后再比较中国和西方在各个技术层级的人数,就可以知道到底有没有差距,差距

程序猿的十层楼(举头望银河,低头撸代码)

这篇博文是很早一个同学推荐的,十分有意思.我觉得时常翻出来过一眼有助于戒骄戒躁. 今天我把它搬到自己的领地,再一次举头仰望....这么燥热的天气,看完凉快了很多~~ 原文的链接:https://www.cnblogs.com/flish/archive/2010/12/04/1896273.html 正文 第1层 菜鸟 第1层楼属于地板层,迈进这层楼的门槛是很低的.基本上懂计算机的基本操作,了解计算机专业的一些基础知识,掌握一门基本的编程语言如C/C++,或者Java,或者JavaScript,

iOS中中UIView头文件详细解析

@interface UIView : UIResponder<NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem> /** *  通过一个frame来初始化一个UI控件 */ - (id)initWithFrame:(CGRect)frame; // YES:能够跟用户进行交互 @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteraction

Storyboard 全解析

XCode 4.3.2 新功能 - Storyboard 最近开始比较有空在玩 XCode 4.3.2,赫然发现它多了个 Storyboard 的东东. Storyboard 这个东西一般来说是在做创意发想的时候,用来将自己的想的一些故事情节画成像是连环漫画一样,想不到 Apple 把它用在这里,真是佩服... 好吧,不废话,先来说说这个 Storyboard 带来什么改变? 在这个版本前,我们在设计画面的时候都是用 interface builder 产生一个 xib 档,然后在 code 要

在iOS中获取UIView的所有层级结构 相关

在iOS中获取UIView的所有层级结构 应用场景 在实际 iOS 开发中,很多时候都需要知道某个 UI 控件中包含哪些子控件,并且分清楚它们的层级结构和自个的 frame 以及 bounds ,以便我们完成复杂的 UI 布局,下面的代码就能很方便的获取某个 UI 控件的所有的层级结构,我们可以用它计算,然后把结果写入到本地磁盘,导出成XML文件,这样我们就可以很直观的看出它内部的细节. /** * 返回传入veiw的所有层级结构 * * @param view 需要获取层级结构的view *

UIButton 解析

IOS之按钮控件--Button全解析及使用 转载自:forget IOS开发中伴随我们始终的 最常用的几个空间之一 -- UIButton 按钮,对于button今天在此做一些浅析,并介绍下主流用法以及常见问题解决办法. 首先是继承问题,UIButton继承于UIControl,而UIControl继承于UIView. 那么UIButton自然继承了UIView的属性.比如frame,layer等 至于UIButton的创建 UIButton *button = [UIButton butto