现在的位置: 首页 > 综合 > 正文

UIView学习笔记

2013年08月04日 ⁄ 综合 ⁄ 共 5870字 ⁄ 字号 评论关闭

UIView是屏幕上矩形面积的界面,用来管理界面上的内容


有以下几点:

    绘图与动画

    布局和子视图管理

    事件处理


视图可以嵌套 形成父-子视图

通常情况下 子视图不是父视图的一部分,也就是说,子视图会覆盖父视图相关区域,超出父视图面积的部分也将显示.

可以通过改变属性clipsToBounds 来取出超出部分的显示,如下:


view添加view,并剪边(UIView属性clipsTobounds的应用)   

如题,有两个view: view1,view2

view1添加view2到其中,如果view2大于view1,或者view2的坐标不在view1的范围内,view2是盖着view1的,
意思就是超出的部份也会画出来


UIView有一个属性,clipsTobounds 默认情况下是NO,

如果,我们想要view2把超出的那部份隐藏起来的话,就得改变它的父视图也就view1的clipsTobounds属性值。

view1.clipsTobounds = YES;


很简单吧,困扰了我好长时间,感谢小三子提供的代码,一不小心发现了。

  UIView的几何大小,有一下属性定义

 frame:定义view的在父view系统坐标系中的原始尺寸,通常用来布局view的大小

 bounds:在不改变大小的情况下,用来调整view的位置

 center:定了view的内部尺寸,专门应用与自定义区域的绘制代码

The
size portion of the frame and bounds rectangles are coupled together so that changing the size of either rectangle updatesthe size of both.

一个矩形区域的大小和边界通常结合起来使用,以改变和更新矩形的大小和位置


创建View

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


增加成为父view的子view

    addSubview:

    insertSubview:aboveSubview:

    insertSubview:belowSubview:

    exchangeSubviewAtIndex:withSubviewAtIndex:


创建一个view时,重要的一点是,要给autoresizingMask这个属性制定一个合适的值,用于view自动调整尺寸.举个例子,就是你如果有很多view叠在一起,如果想同时放大缩小它们,可以用这个方法.


For example, calling the setNeedsLayoutmethod
forces your view to update its layout.

当调用方法setNeedsLayout时,视图会自动重绘.


视图的重绘周期

在以下情况下会重绘:

view第一次显示时

view的部分或所有可见性改变导致布局发生变化时

系统要求view重新绘制内容时

For views that contain custom content using UIKit or Core Graphics, the system calls the view’s drawRect: method.

当view包含自定义内容时,系统调用view的函数drawRect:进行绘制.


需要自定义绘图时,子类需要重写drawRect:方法


当view的真实内容发生改变时,你需要通知系统去重绘.you
do this by calling your v iew’ssetNeedsDisplay or setNeedsDisplayInRect: method
of the view.


这两个方法让系统知道,视图需要立即更新(在下一次更新周期前,正常情况下,view是等待下一次更新周期进行更新的).

Note: If you are using OpenGL ES to do your drawing, your view’s drawRect:method
is not called. Instead, it is up to you to determine when your view needs to be redrawn and initiate the appropriate drawing updates. For more information about how to draw using OpenGL ES, see OpenGL
ES Programming Guide for iOS
.



动画

改变几个属性,就可以让view具有动画效果.改变一些属性创建动画,并将这些改变在短时间内传达给用户.

UIView自身对动画做了很多工作,但是你仍然需要决定决定改变哪个属性,需要哪种动画.

There are two different ways to initiate animations:

以下这些属性是可动画的:

The following properties of the UIView class
are animatable:


线程的考虑

您的应用程序的用户界面的操作必须出现在主线程。因此,你应该始终在应用程序的主线程中运行的代码中调用UIView的方法。唯一的例外是在创建视图对象本身时,但所有其他的操作应该发生在主线程上。


子类化的注意事项

UIView是个高度化可配置的类

子类化的时候 需要根据实际需求覆盖一些方法.

可以覆盖的方法有以下几方面:

  • Initialization:

    • initWithFrame:—It
      is recommended that you implement this method. You can also implement custom initialization methods in addition to, or instead of, this method.

    • initWithCoder:—Implement
      this method if you load your view from an Interface Builder nib file and your view requires custom initialization.

    • layerClass—Implement
      this method only if you want your view to use a different Core Animation layer for its backing store. For example, if you are using OpenGL ES to do your drawing, you would want to override this method and return the 
      CAEAGLLayer class.

  • Drawing and printing:

    • drawRect:—Implement
      this method if your view draws custom content. If your view does not do any custom drawing, avoid overriding this method.

    • drawRect:forViewPrintFormatter:—Implement
      this method only if you want to draw your view’s content differently during printing.

  • Layout:

    • sizeThatFits:—Implement
      this method if you want your view to have a different default size than it normally would during resizing operations. For example, you might use this method to prevent your view from shrinking to the point where subviews cannot be displayed correctly.

    • layoutSubviews—Implement
      this method if you need more precise control over the layout of your subviews than the autoresizing behaviors provide.

    • didAddSubview:willRemoveSubview:—Implement
      these methods as needed to track the additions and removals of subviews.

    • willMoveToSuperview:didMoveToSuperview—Implement
      these methods as needed to track the movement of the current view in your view hierarchy.

    • willMoveToWindow:didMoveToWindow—Implement
      these methods as needed to track the movement of your view to a different window.

  • Event Handling:


子类化的候选方案

大部分类的行为是可配置的,不需要子类化,在子类化之前,考虑以下属性和行为是否能提供满足你的需要:

  • Set a value for the autoresizingMask property.
    This property provides automatic layout behavior when the superview’s frame changes.

  • Set a value for the contentMode property.
    This property determines the layout behavior of the view’s content. This property also affects how the content is scaled to fit the view and whether it is cached or redrawn.

  • Set a value for the contentStretch property.
    This property is typically used to implement buttons and other resizable views. Defining a stretchable area lets the system handle any drawing associated with size changes to the view.

  • Set a value for the hidden or alpha property.
    These properties change the transparency of the view as a whole and avoid the need for drawing your content with transparency.

  • Set a value for the backgroundColor property.
    This property sets the view’s overall color and avoids the need to draw that color yourself in the 
    drawRect:method.

  • Add subviews. Rather than draw your content using a drawRect: method,
    embed image and label subviews with the content you want to present.

  • Add one or more gesture recognizers. Rather than subclass to intercept and handle touch events yourself, you can use gesture recognizers to send anaction
    message
     to a target object.

  • Use the built-in animation support rather than trying to animate changes yourself. The animation support provided by Core Animation is fast and easy to use.

  • Use an image for the view’s background. For views that display relatively static content, consider using a UIImageView object
    with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic 
    UIView object
    and assign your image as the content of the view’s
    CALayer object.




Tasks

Initializing a View Object

Configuring a View’s Visual Appearance

    抱歉!评论已关闭.