UIView学习笔记

来源:互联网 发布:美少女万华镜 for mac 编辑:程序博客网 时间:2024/06/07 16:19

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:

  • In iOS 4 and later, use the Block-based animation methods. (Recommended)(such asanimateWithDuration:animations:)

  • Use the begin/commit animation methods. (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:

  •   @property frame

  •   @property bounds

  •   @property center

  •   @property transform

  •   @property alpha

  •   @property backgroundColor

  •   @property contentStretch


线程的考虑

您的应用程序的用户界面的操作必须出现在主线程。因此,你应该始终在应用程序的主线程中运行的代码中调用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:

    • touchesBegan:withEvent:touchesMoved:withEvent:,touchesEnded:withEvent:touchesCancelled:withEvent:—Implement these methods if you need to handle touch events directly. (For gesture-based input, use gesture recognizers.)


子类化的候选方案

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

  • 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’sCALayer object.




Tasks

Initializing a View Object

  • – initWithFrame:

Configuring a View’s Visual Appearance

  •   backgroundColor  property
  •   hidden  property
  •   alpha  property
  •   opaque  property
  •   clipsToBounds  property
  •   clearsContextBeforeDrawing  property
  • + layerClass
  •   layer  property

Configuring Event-Related Behavior

  •   userInteractionEnabled  property
  •   multipleTouchEnabled  property
  •   exclusiveTouch  property

Configuring the Bounds and Frame Rectangles

  •   frame  property
  •   bounds  property
  •   center  property
  •   transform  property

Configuring the Resizing Behavior

  •   autoresizingMask  property
  •   autoresizesSubviews  property
  •   contentMode  property
  •   contentStretch  property
  • – sizeThatFits:
  • – sizeToFit

Managing the View Hierarchy

  •   superview  property
  •   subviews  property
  •   window  property
  • – addSubview:
  • – bringSubviewToFront:
  • – sendSubviewToBack:
  • – removeFromSuperview
  • – insertSubview:atIndex:
  • – insertSubview:aboveSubview:
  • – insertSubview:belowSubview:
  • – exchangeSubviewAtIndex:withSubviewAtIndex:
  • – isDescendantOfView:

Laying out Subviews

  • – layoutSubviews
  • – setNeedsLayout
  • – layoutIfNeeded

Drawing and Updating the View

  • – drawRect:
  • – setNeedsDisplay
  • – setNeedsDisplayInRect:
  •   contentScaleFactor  property

Formatting Printed View Content

  • – viewPrintFormatter
  • – drawRect:forViewPrintFormatter:

Managing Gesture Recognizers

  • – addGestureRecognizer:
  • – removeGestureRecognizer:
  •   gestureRecognizers  property

Animating Views with Blocks

  • + animateWithDuration:delay:options:animations:completion:
  • + animateWithDuration:animations:completion:
  • + animateWithDuration:animations:
  • + transitionWithView:duration:options:animations:completion:
  • + transitionFromView:toView:duration:options:completion:

Animating Views

Use of the methods in this section is discouraged in iOS 4 and later. Use the block-based animation methods instead.

  • + beginAnimations:context:
  • + commitAnimations
  • + setAnimationStartDate:
  • + setAnimationsEnabled:
  • + setAnimationDelegate:
  • + setAnimationWillStartSelector:
  • + setAnimationDidStopSelector:
  • + setAnimationDuration:
  • + setAnimationDelay:
  • + setAnimationCurve:
  • + setAnimationRepeatCount:
  • + setAnimationRepeatAutoreverses:
  • + setAnimationBeginsFromCurrentState:
  • + setAnimationTransition:forView:cache:
  • + areAnimationsEnabled

Identifying the View at Runtime

  •   tag  property
  • – viewWithTag:

Converting Between View Coordinate Systems

  • – convertPoint:toView:
  • – convertPoint:fromView:
  • – convertRect:toView:
  • – convertRect:fromView:

Hit Testing in a View

  • – hitTest:withEvent:
  • – pointInside:withEvent:

Ending a View Editing Session

  • – endEditing:

Observing View-Related Changes

  • – didAddSubview:
  • – willRemoveSubview:
  • – willMoveToSuperview:
  • – didMoveToSuperview
  • – willMoveToWindow:
  • – didMoveToWindow















原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 染了深褐色很黑怎么办 路边停车费没交怎么办 3岁宝宝难入睡怎么办 一上火眼睛就肿怎么办 孩子上火眼睛红有眼屎怎么办 孩子眼屎多又黄怎么办 眼睛皮周围红痒怎么办 新买的拖鞋有味怎么办 毛巾变得滑滑的怎么办 买的挂钩粘不住怎么办 吸墙挂钩吸不住怎么办 沾挂钩不粘了怎么办 粘钩掉了不粘了怎么办 贴墙挂钩粘不住怎么办 月经量大血块多怎么办 23岁乳房小扁平怎么办 十六岁基本没胸怎么办 肚子上的肉松弛怎么办 17岁乳房外扩该怎么办 胸下垂严重怎么办 17岁 棉条超过8小时了怎么办 在学校来了月经怎么办 如果在学校来月经怎么办 来月经流血量大怎么办 非经期出血量多怎么办 在学校来月经了怎么办 月经好久不来了怎么办 例假推迟了20天怎么办 月经晚了13天了怎么办 大姨吗推迟十天怎么办 月经推迟了3个月怎么办 例假推迟了15天怎么办 来月经同了房怎么办呢 来月经同了房该怎么办 21岁天生贫乳怎么办 吃了一点苦瓠子怎么办 运动内衣买小了怎么办 硅胶胸贴容易掉怎么办 穿文胸老是空杯怎么办 喝啤酒眼睛肿了怎么办 全身皮肤太干了怎么办