iOS 官方文档整理----UIView

来源:互联网 发布:淘宝天猫投诉有用吗 编辑:程序博客网 时间:2024/05/17 01:19
继承:  UIResponder : NSObject
框架: UIKit
作用:  
      1> 绘图和动画 使用UIKit, Core Graphics, and OpenGL ES绘制内容.
     2> 布局自控件
      3> 事件处理  能处理触摸事件和其他定义在UIResponder 的类
      4> 用 addGestureRecognizer: 这个方法进行手势识别

父控件和子控件: 一个父控件可以包含多个子控件,但是子空间只能有一个父控件.可以通过clipsToBounds来设置边界部分.默认为NO.

几何属性: frame bounds center

创建UIView
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(100100100100)];
    myView.
backgroundColor = [UIColor purpleColor];
    [
self.view addSubview:myView];
子空间之间的关系:
     insertSubview:aboveSubview:
    insertSubview:belowSubview:
    exchangeSubviewAtIndex:withSubviewAtIndex:

autoresizingMask属性
保证view正确的改变大小.比如用 setNeedsLayout方法是强制view更新自己的布局

视图周期
当一个视图第一次出现,全部或者部分可见的时候,系统会询问view来绘制其上下文.对应使用UIKit或Core Graphics框架的自定义view,系统在view的内容显示到当前的上下文上之前调用 drawRect:方法.
当view改变时,要调用 setNeedsDisplay 或者 setNeedsDisplayInRect:通知系统view要重绘.
注意: 当使用Open GL ES时,要使用GLKView

动画
两种方式开始动画:
    1> block  (IOS4以后)
    2> begin/commit animation
可使用动画的属性:
  •  @property frame

  •   @property bounds

  •   @property center

  •   @property transform

  •   @property alpha

  •   @property backgroundColor

  •   @property contentStretch

线程问题
更新UI必须在主线程

重写(Methods to Override)
    1> 初始化
       initWithFrame: 建议使用这个方法,当然你也可以自定义这个方法
       initWithCoder: 如果从IB 的nib中加载的view,那么自定义初始化view需要实现这个方法
       layerClass:     如果你想使用自定义的Core Animation,需要覆盖此方法
    2> 绘制和打印
       drawRect:           自定义的时候实现此方法.如果不做任何自定义,避免重写此方法.
       drawRect:forViewPrintFormatter:   当你想要在打印过程中绘制不同的view content

约束条件(constraint)
    1> requiresConstraintBasedLayout   如果你的view需要约束条件才能正确工作,实现这个方法.
        2>  updateConstraints  如果你的子视图之间需要创建自定义约束.
         3>  alignmentRectForFrame:frameForAlignmentRect: 视图之间的位置关系


布局(layout)
         1> sizeThatFits: 当你想view在改变大小的时候不是默认的大小.比如你可以使用这个方法来防止子视图在缩小过程中不能正确显示.
         2> layoutSubviews 当你想精确的控制子视图的布局(而不是约束和autoresizing)
         3> didAddSubview:willRemoveSubview:跟踪子视图的添加和删除操作
         4> willMoveToSuperview:didMoveToSuperview 在视图层次中踪当前视图的跳转
         5> willMoveToWindow:didMoveToWindow 跟踪view跳到不同的window


事件处理(event handling)
        1> touchesBegan:withEvent:touchesMoved:withEvent:touchesEnded:withEvent:touchesCancelled:withEvent: 触摸事件(对于基于手势的输入,使用 gesture recognizers)
        2> gestureRecognizerShouldBegin: 如果你view需要直接处理touch事件,或者防止附着的手势触发其他操作.


0 0
原创粉丝点击