视图编程指南View Programming Guide for iOS-2

LabVIew串口收发的一些问题 LabView串口收发实验总结 阅读详情

三、Views 视图

(一) 创建和配置视图对象  Creating and Configuring View Objects

1, 使用IB创建视图对象    Creating View Object Using Interface Builder:

所见即所得,你在一个nib file中保存这些live objects,nib file是一个资源文件,它保存了你的对象的配置和状态。

一般nib file的最高级视图是一个单独的视图,代表viewController’s view属性(ViewController本身是nib file的File’s Owner)。 顶级View应该根据目标设备设置适当的尺寸,并包含所有其他的视图 . 很少使用一个nib file只存储你的view controller’s view的一部分结构。

如果你的nib file没有和一个viewController关联,你可以通过一个NSBundle or UINib对象来手动加载他。

2,通过代码创建视图    Creating View Objects Programmatically

你可以使用标准的allocation/initialization 格式。默认的初始化方法是 initWIthFrame:

如下:

CGRect  viewRect = CGRectMake(0, 0, 100, 100);

UIView* myView = [[UIView alloc] initWithFrame:viewRect];

3,设置视图的属性  Setting the Properties of a View:

UIView有一些声明的属性来控制view的外观和行为。你也可以在IB中的Inspector window修改这些属性。

属性alpha,hidden,opaque的使用:

属性bounds,frame,center,transform的使用:

属性autoresizingMask,autoresizesSubviews的使用:

属性contentMode,contentStretch,contentScaleFactor的使用:

属性gestureRecognizers,userInteractionEnabled,multipleTouchEnabled,exclusiveTouch的使用:这些属性影响view如何处理触摸事件。

属性backgroundColor,subviews,drawRect:method,layer,(layerClass method)的使用:

4,设置视图的tag属性 Tagging View for Future Identification:

使用view的tag属性来唯一标识view。该属性默认为0,是一个int值。 使用viewWithTag:方法来根据tag值查找view。这个方法使用depth-first查找目标和他的subviews,他不查找superview和其他的view。


(二)创建和管理视图架构  Creating and Managing a View Hierarchy

1,添加和移除子视图 Adding and Removing Subviews:

1) addSubview:方法来添加一个subview

2) insertSubview:…

3) 重新排列:bringSubviewToFront:  sendSubviewToBack 或者 exchangeSubviewAtIndex:withSubviewAtIndex:

4) removeFromSuperview

 

当添加一个subview到他的父view,subview的当前frame矩形指示他的初始位置为相对于其父视图的坐标。如果他的frame在他的superview的可见bounds之外,默认地不被clipped,如果想被clipped,则设置superview的clipsToBounds属性为YES。(但是父view外面的部分的触摸事件永远不会被传递)。

一般在application:didFinishLaunchingWithOptions:方法中给window对象addSubview

或者在loadView或viewDidLoad方法中给自定义的view添加subview。

当你添加一个subview到另一个view中,UIKit通知both the parent and the child views of the change.如果你实现了自定义的视图,你可以窃听这些通知后面的方法中的一个或多个: willMoveToSuperview:  willMoveToWindow:  willRemoveSubview:  didAddSubview:  didMoveToSuperview  or  didMoveToWindow 方法。你可以利用这些通知来更新任何状态信息和其他附加操作。

每个视图的window属性表示他们正在哪个窗口中被显示。因为视图层次结构中的root view没有父,所以他的superview属性为nil. 对于屏幕上的views来说,window对象是视图层次结构的根视图(root view)

2,隐藏视图 Hiding Views:

设置hidden属性为YES或更改alpha值为0.0。一个被隐藏的视图不接受触摸事件。然而,隐藏的视图却参与autoresizing和其他的布局操作。因此,经常隐藏一个视图,而不是移除一个视图 。尤其是你计划过会再次显示view时。

重要提示:如果你隐藏的view是当前第一响应者,view不会自动回收他的first responder状态。以first responder为目标的事件仍然传递给被隐藏的view。为了避免这样的事情发生,你应该在隐藏视图时,强制收回其第一响应者状态。

如果你想动画,你需要设置view的alpha属性,hidden属性不是动画属性。

3,Locating Views in a View Hierarchy  定位view

有两种方法定位view

1)把相关view的指针存储在合适的位置,例如存储在拥有view的viewController中。

2)为每一个view的tag属性指定一个唯一的integer值,并且使用viewWithTag:方法来定位。

第一种方法最常使用。

4,转换、缩放和旋转视图  Translating,Scaling,and Rotating Views

每个View都有一个相关的affine transform,你可以用他来translate,scale,or rotate the view’s content。

UIView的transform属性包含一个CGAffineTransform结构 with the transformations to apply。默认地,这个属性被设置为identity transform(标准矩阵),不修改view的外观。你可以在任何时候指定一个新的transform。例如,要旋转一个view 45度,你可以使用下面的代码:

CGAffineTransform xform = CGAffineTransformMakeRotation(M_PI/4.0);
self.view.transform = xform;

应用上面的代码到一个view,他会沿中心点顺时针旋转view。

当应用多个transformations到一个view上,添加顺序是关键的。所有的动画都是以view的中心点为基础的。

5,Converting Coordinates in the View Hierarchy 转换坐标系

例如,触摸事件报告每个触摸的位置—以window’s的坐标系统为基准。但是view对象经常需要 在视图内的本地坐标信息。UIView定义了下面的方法来转换坐标到视图的本地坐标。(或将本地坐标转换为另外一个视图的坐标):

convertPoint:fromView:

convertRect:fromView:

convertPoint:toView:

convertRect:toView:

如果指定view参数为nil,则表示将坐标转换为包含view的windowconvert to or from the window that contains the view。

除了UIView 转换方法之外,UIWindow类也定义了一些方法。

convertPoint:fromWindow:

convertRect:fromWindow:

convertPoint:toWIndow:

convertRect:toWIndow:

 

当在被旋转的view中转换坐标,UIKit converts rectangles under the assumption(假定,设想) that you want the returned rectangle to reflect the screen area covered by the source rectangle. Figure 3-3 shows an example of how rotations can cause the size of the rectangle to change during a conversion.在图中,外部的parent view包含一个被旋转的subview。转换在subview的坐标系中的rectangle到他的parent坐标系中产生了一个更大的rectangle。这个大的rectangle实际上是outerView的最小rectangle—包含the rotated  rectangle的最小rectangle。

视图编程指南View Programming Guide for iOS-2 - supershll - 记忆里
 

(二)Adjusting the Size and Position of Views at Runtime:调整大小和位置

1,Being Prepared for Layout Changes: 下列事件发生时,布局将发生更改发生:

1)view的bounds的size发生变化

2)界面方向发生变化,这通常导致root view的bounds的rectangle发生变化

3)视图的layer的sublayer的Core Animation set发生更改并且需要布局时。

4)你的程序通过调用视图的SetNeedsLayout或layoutIfNeeded方法强迫其layout发生

5)你的程序调用视图下面的layer对象的setNeedsLayout方法强迫布局layout发生

2,通过Autoresizing Mask处理布局更改 Handling Layout Changes Automatically Using Autoresizing Rules

使用superview的autoresizesSubviews属性来决定是否调整视图的大小。如果这个属性是YES,superview使用subview的autoresizingMask属性来决定如何size和position subview。


AutoresizeMask常量:

UIViewAutoresizingNone: 不进行autoresize。

UIViewAutoresizingFlexibleHeight: The view’s height changes when the superview’s height changes. If this constant is not included, the view’s height does not change.

UIViewAutoresizingFlexibleWidth: The view’s width changes when the superview's width changes. If this constant is not included, the view’s width does not change.

UIViewAutoresizingFlexibleLeftMargin: The distance between the view’s left edge and the superview’s left edge grows or shrinks as needed. If this constant is not included, the view’s left edge remains a fixed distance from the left edge of the superview.

UIViewAutoresizingFlexibleRightMargin:The distance between the view’s right edge and the superview’s right edge grows or shrinks as needed. If this constant is not included, the view’s right edge remains a fixed distance from the right edge of the superview.

UIViewAutoresizingFlexibleBottomMargin:

UIViewAutoresizingFlexibleTopMargin:

视图编程指南View Programming Guide for iOS-2 - supershll - 记忆里
 
最早的方法配置autoresizing rules 是使用IB 中的Size Inspector.然而,the behavior and use of margin indicators is effectively reversed。
3,Tweaking (扭,捏) the layout of your views Manually(手动地)

当view的size发生变化时,UIKit应用autoresizing行为到subview,然后调用view的layoutSubviews方法进行手动的更改。当autoresizing行为不能满足你的要求时,你可以在自定义view中实现这个方法。你可以做下面的事情:

1)调整任何immediate subviews的size和position

2)添加或移除subviews或Core Animation Layers

3) 通过调用setNeedsDisplay或setNeedsDisplayInRect:方法强迫subview重绘。

应用程序在实现一个大的可滚动区域的时候经常手动地指定子视图 。因为为可滚动的内容只准备一个大的视图是不现实的,应用经常实现一个root view,包含很多小的tile views(瓦片视图)。每个瓦片代表scrollable content的一部分。当一个scroll event发生时,root view调用他的setNeedsLayout方法来发起一个布局的改变。他的layoutSubviews方法然后基于发生的滚动重新放置瓦片视图。对于滑出可视区域的tile view,layoutSubviews方法移动瓦片到 the incoming edge,replacing their contents in the process.

当你写layout code时,确保测试你的代码使用下面的方法:

1)改变你的view的方向,来确保在所有支持的界面和方向布局看起来都是正确的。

2)确保你的代码正确响应status bar的高度的变化。当打电话时,the status bar 的高度增长,打完电话后,the status bar 的高度降低。

(三)在运行时修改视图 Modifying Views at Runtime:

1)在一个视图控制器中 In a View Controller:

       a)viewController在view显示之前创建他们,当view不再需要时,他负责除掉他们。

       B)当设备更改方向时,viewController可能会调整视图的尺寸和位置。作为调整的一部分,他可能会显示或隐藏一些view

       C)当viewController管理可编辑的内容,他可能会在进入或离开编辑模式时调整view结构。例如,他可能会添加额外的button或其他控制来帮助编辑各种不同的内容点。这可能会需要resizing已经存在的views来适应附加的控制。

2)在动画块中 In Animation blocks:

       a)当你想transition(过渡,转变) between different sets of views in your user interface,you hide some views and show others from inside an animation block.

       b)当实现特定的效果时,你可能使用动画块修改view的许多属性。例如,动画view的尺寸时,你会更改他的frame rectangle的尺寸。

3)Other ways:

       a)当触摸事件或gesture(手势)发生,your interface might respond by loading a new set of views or changing the current set of views.

       B)当用户与一个scroll view交互时,a large scrollable area might hide and show tile subviews.

       c)当键盘显示时,你可能需要reposition or resize views so that 他们不显示在键盘下面.

 

ViewController是初始化你的view的更改的普通位置。他基本上负责发生在view上的所有事情。如果你支持edit mode,viewController的setEditing:animated:方法给你机会在进入editable模式时更改你的view。

动画块是另外一个初始化view的更改的位置。UIView内置的动画支持使得动画view的属性变化变得容易。你可以使用

transitionWithView:duration:options:animations:completion: 或

transitionFromView:toView:duration:options:completion: 方法to swap out entire sets of views for new ones。

 

(四)与Core Animation Layers 交互 Interacting with Core Animation Layers:

每个view都有一个dedicated(专用的,专注的) Core Animation Layer。使用view’s layer属性获得。

1,Changing the Layer Class Associated with a View

在view创建之后,他的layer类型就不能再被更改。因此,每个view使用layerClass 类方法来指定他的layer对象的类。此方法的默认实现返回CALayer类,并且唯一更改这个值的方法是 子类化并且override方法,并返回一个不同的值。你可能会想更改这个值in the following circumstances(环境,情况,遭遇):

1)你的应用使用OpenGL ES绘图,layer必须是CAEAGLLayer class。

2)你的view使用tiling(瓦片)来显示一个large scrollable area,这时你可能会想使用CATiledLayer

代码如下:

+ (Class)layerClass

{

    return [CAEAGLLayer class];

}

每个View在被初始化时调用这个方法,并使用返回的class创建他的layer object。另外,view总是指定自身为他的layer object的delegate。这时,view拥有他的layer且view和layer之间的关系不能更改。你不可以指定同一个view作为另一个layer object的delegate。更改这种关系会导致绘图问题并很可能使你的应用崩溃。

 

2,视图中内嵌的Layer对象 Embedding Layer Object in a View:

如果你选择主要使用layer object替代view来工作,你可以incorporate(合并) custom layer object into your view hierarchy as needed。 A custom layer object is any instance of CALayer that is not owned by a view。You typically create custom layers programmatically and incorporate then using Core Animation routines(惯例).Custom Layers 不接收事件或参与响应链,但是确实绘制自身并响应size changes in their parent view or layer according to the Core Animation rules。

下面的代码显示 view controller that creates a custom layer object and adds it to its root view。The layer is used to display a static image that is animated.instead of adding the layer to the view itself,you add it to the view’s underlying layer。

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    // Create the layer.

    CALayer* myLayer = [[CALayer alloc] init];

 

    // Set the contents of the layer to a fixed image. And set

    // the size of the layer to match the image size.

    UIImage layerContents = [[UIImage imageNamed:@"myImage"] retain];

    CGSize imageSize = layerContents.size;

 

    myLayer.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height);

    myLayer = layerContents.CGImage;

 

    // Add the layer to the view.

    CALayer*    viewLayer = self.view.layer;

    [viewLayer addSublayer:myLayer];

 

    // Center the layer in the view.

    CGRect        viewBounds = backingView.bounds;

    myLayer.position = CGPointMake(CGRectGetMidX(viewBounds), CGRectGetMidY(viewBounds));

 

    // Release the layer, since it is retained by the view's layer

    [myLayer release];

}

 

你可以添加任意数量的sublayer并且安排他们进入sublayer层次结构(hierarchy)。这些layers必须附属于view的layer object。

(五)Difining a Custom View

1,custome view

custome view的工作是展示内容和管理交互。成功的实现不仅包括绘图和处理事件,下面的checklist(清单)包括了更多重要的方法你可以override:

1)Define the appropriate initialization methods for your view:

       a)initWithFrame: 通过代码创建时需要的方法

       b)initWithCoder: 通过nib创建时需要的方法.

2)dealloc 方法负责清理操作

3)drawRect: 负责绘图

4) 设置autoresizingMask属性

5)如果你的view管理一个或多个integral(积分的,完整的,整体的) subviews,

       a)在你的view的初始化序列中创建这些subviews

       b)设置他们的autoresizingMask

       c)如果你的subviews需要custom layout,override the layoutSubviews方法并实现你的layoutCode

6)处理基于触摸的事件:

       a)附加任何适当的手势识别到view上,通过使用addGestureRecognizer:方法。

       B)如果你想自己处理触摸,覆盖 touchesBegan:withEvent:, touchesMovedWithEvent:, touchesEndedLwithEvent:, and  touchesCancelled:WithEvent:方法。

7)如果你想在打印的时候看起来不同,实现drawRect:forViewPrintFormatter:方法.

对于overriding methods的补充是,别忘了你可以使用view的已经存在的属性做很多事情,例如,contentMode和contentStretch属性。还有CALayer object等。

 

2,Initalizing Your Custom View:

你应该至少定义一个initWithFrame:初始化方法.

 

- (id)initWithFrame:(CGRect)aRect {

    self = [super initWithFrame:aRect];

    if (self) {

          // setup the initial properties of the view

          ...

       }

    return self;

}

 

如果你想使用nib file加载你的view,你应该使用initWithCoder:方法,并遵循NSCoding协议.

即使你采用了NSCoding协议,IB仍然不知道你的view的自定义属性,因此不能encode(编码)这些属性到nib file中。结果是,你的initWithCoder:方法should perform whatever initialization code it can to put the view into a known state。你还可以实现awakeFromNib方法,并且使用这个方法执行附加初始化。

 

3,Implementing Your Drawing Code:

示例代码:A Drawing Method

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect    myFrame = self.bounds;

 

    // Set the line width to 10 and inset the rectangle by

    // 5 pixels on all sides to compensate for the wider line.

    CGContextSetLineWidth(context, 10);

    CGRectInset(myFrame, 5, 5);

 

    [[UIColor redColor] set];

    UIRectFrame(myFrame);

}

 

如果你知道view的绘图代码覆盖整个view的surface with opaque content,你可以改进系统性能by setting view的 opaque属性为YES.

另一个way to improve drawing 性能,特别是during scrolling,是设置view的clearsContextBeforeDrawing属性为YES。此时,UIKit自动填充区域 to be updated by your drawRect:方法 with 透明的黑色 before calling your method。

 

4,Responding to Events

除了直接处理触摸事件,你可以使用addGestureRecognizer:方法。The gesture recognizer然后调用你的action method when the corresponding gesture occurs。

如果你选择直接处理触摸事件,你可以实现

touchesBegan  touchesMoved  touchesEnded  touchesCancelled

 

view默认响应only one touch at a time。如果你使用两个手指,系统会忽略触摸事件并且不报告事件给你的view。如果你打算跟踪多手指探测,你需要设置view的multipleTouchEnabled属性为YES。

一些view,如labels or images,禁用事件处理 altogether initially.你可以控制一个view是否接收触摸事件—通过更改view的userInteractionEnabled属性。你可能会临时设置其为NO来组织处理事件在你的view处理一个耗时操作时组织事件到达你的view,你还可以使用UIApplication的beginIgnoringInteractionEvents和endIgnoringInteractionEvents方法。这些方法影响整个application。

注意:UIView的动画方法禁用触摸事件—在动画正在进行中。你可以override这个行为通过设置animation appropriately.参看 “Animations”

 

As it handles touch events,UIKit使用UIView的hitTest:withEvent:和pointInside:withEvent:方法来决定是否一个触摸事件发生在给定的view的bounds内部。虽然你很少需要override这些方法,但是你可以这么做使得implement custom touch behaviors for your view。例如,你可以override这些方法来阻止subviews处理触摸事件。

 

5,Cleaning up after your view

四、动画 Animations

(一)What can be animated?

UIKit和Core Animation都提供了对动画的支持,但是the level of support provided by each technology varies。

在UIKit中,animations通过UIView对象执行。

下表列出了可动画的属性—UIView类的内建的动画支持。可动画不表示动画会自动执行。为了动画这些属性改变,你必须更改这些属性在inside an animation block。


可动画的属性

frame

bounds

center

transform

alpha

backgroundColor

contentStretch

 

如果你想执行更复杂的动画,或UIView不支持的动画,你可以使用core animation和view的underlying layer来创建动画。因为view和layer对象混乱地结合在一起,更改view的layer影响view本身。使用Core Animation,你可以动画下列类型的更改 for your view’s layer:

1)the size and position of the layer

2)the center point used when performing transformations

3)Transformations to the layer or its sublayer in 3D space

4)The addition or removal of a layer from the layer hierarchy

5)the layer’s Z-order relative to other sibling layers

6)The layer’s shadow

7)the layer’s border(including whether the layer’s corners are rounded)

8)The portion of the layer that stretches during resizing operations

9)the layer’s opacity

10)The clipping behavior for sublayers that lie outside the layer’s bounds

11)the current contents of the layer

12)the rasterization(光栅化) behavior of the layer

 

(二)Animating Property Changes in a view:

你必须wrap(包裹)这些更改到一个动画块内。

The term animation block is used in the generic sense to refer to any code that designates animatable changes。在iOS4以后,你使用block objects创建一个动画块。在早期版本中,你标志一个动画块的起始和结束—使用UIView的特定类方法。

 

1,Starting Animations Using the Block-Based Methods

有几个block-based方法提供了不同级别的动画块的配置。他们是

animateWithDuration:animations:

animateWithDuration:animations:completion:

animateWithDuration:delay:options:animations:completion:

 

因为他们是类方法,所以这些动画块are not tied to a single view。因此,你可以使用这些防范创建一个单独的动画 that involves changes to multiple views。如下面的代码所示:


[UIView animateWithDuration:1.0 animations:^{

        firstView.alpha = 0.0;

        secondView.alpha = 1.0;

}];

 

上面的动画会立即执行一次,如果你想更改默认参数,你必须使用animateWithDuration:delay:options:animations:completion:,你可以自定义下面的参数:

1)delay

2)The type of timing curve(使弯曲) to use during the animation

3) 重复次数

4)动画是否reverse itself automatically when it reaches the end

5)是否触摸事件传递给views 在动画执行中

6)是否动画应该打断正在执行的动画还是等待他们完成再开始。

 

另外一个事情--- animateWithDuration:animations:completion:和animateWithDuration:delay:options:animations:completion:都支持的是指定一个completion handler block(动画执行完之后的block)。你可能会使用这个block来告诉你的应用动画已经执行完毕。Completion Handler也是连接单独的动画到一起的方法。

下面是示例代码:

 

- (IBAction)showHideView:(id)sender

{

    // Fade out the view right away

    [UIView animateWithDuration:1.0

        delay: 0.0

        options: UIViewAnimationOptionCurveEaseIn

        animations:^{

             thirdView.alpha = 0.0;

        }

        completion:^(BOOL finished){

            // Wait one second and then fade in the view

            [UIView animateWithDuration:1.0

                 delay: 1.0

                 options:UIViewAnimationOptionCurveEaseOut

                 animations:^{

                    thirdView.alpha = 1.0;

                 }

                 completion:nil];

        }];

}

 

2,Starting Animations Using the Begin/Commit Methods

在iOS 3.2及更早期版本中,你必须使用UIView的beginAnimations:context:和commitAnimations类方法来定义你的动画块。在这些方法之间的任何可动画的属性更改被动画 after you call commitAnimations方法。动画执行在另一个线程中。

下面是示例代码:

    [UIView beginAnimations:@"ToggleViews" context:nil];

    [UIView setAnimationDuration:1.0];

 

    // Make the animatable changes.

    firstView.alpha = 0.0;

    secondView.alpha = 1.0;

 

    // Commit the changes and perform the animation.

    [UIView commitAnimations];

 

默认地,在一个动画块内的所有可动画属性更改一起被动画。如果你想只动画一些属性,使用setAnimationsEnabled:方法来临时禁用动画,然后做出你不想动画的属性更改,然后再次调用setAnimationsEnabled:方法来启用动画。你可以使用areAnimationsEnabled类方法来确定动画是否当前启用。

 

3,Configuring the Parameters for Begin/Commit Animations

方法setAnimationStartDate: setAnimationDelay: 设置动画何时开始执行,默认值是0

方法setAnimationDuration: 动画执行时间

方法setAnimationCurve: 设置动画时间曲线,是线性执行还是在某一时间改变速度

方法setAnimationRepeatCount: setAnimationRepeatAutoreverses: 设置动画重复次数还是来回执行

方法 setAnimationDelegate:  setAnimationWIllStartSelector: setAnimationDidStopSelector:  设置动画执行前和执行后的方法

方法setAnimationBeginsFromCurrentState: 使用这个方法立即停止所有之前的动画并且从停止点开始新动画,如果你设置NO替代YES,则新的动画等待之前的动画执行完才开始。

 

示例代码:

// This method begins the first animation.

- (IBAction)showHideView:(id)sender

{

    [UIView beginAnimations:@"ShowHideView" context:nil];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

    [UIView setAnimationDuration:1.0];

    [UIView setAnimationDelegate:self];

    [UIView setAnimationDidStopSelector:@selector(showHideDidStop:finished:context:)];

 

    // Make the animatable changes.

    thirdView.alpha = 0.0;

 

    // Commit the changes and perform the animation.

    [UIView commitAnimations];

}

 

// Called at the end of the preceding animation.

- (void)showHideDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

{

    [UIView beginAnimations:@"ShowHideView2" context:nil];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    [UIView setAnimationDuration:1.0];

    [UIView setAnimationDelay:1.0];

 

    thirdView.alpha = 1.0;

 

    [UIView commitAnimations];

}


4,Configuring an Animation Delegate:

setAnimationDelegate: setAnimationWillStartSelector: setAnimationDidStopSelector:

 

delegate方法是:

- (void)animationWillStart:(NSString *)animationID context:(void *)context;

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;

animationID和context参数是你传递给beginAnimations:context:方法的参数.

 

setAnimationDidStopSelector: selector方法有一个附加的参数,一个布尔值,YES表示动画执行完,如果NO表示动画要么被cancel了要么过早地被另外一个动画停止了。

 

5,Nesting(筑巢) Animation Blocks

把一个动画加入到另一个动画中,nested动画继承其父动画的属性,也可以override,动画同时执行。

示例代码:

[UIView animateWithDuration:1.0

        delay: 1.0

        options:UIViewAnimationOptionCurveEaseOut

        animations:^{

            aView.alpha = 0.0;

 

            // Create a nested animation that has a different

            // duration, timing curve, and configuration.

            [UIView animateWithDuration:0.2

                 delay:0.0

                 options: UIViewAnimationOptionOverrideInheritedCurve |

                          UIViewAnimationOptionCurveLinear |

                          UIViewAnimationOptionOverrideInheritedDuration |

                          UIViewAnimationOptionRepeat |

                          UIViewAnimationOptionAutoreverse

                 animations:^{

                      [UIView setAnimationRepeatCount:2.5];

                      anotherView.alpha = 0.0;

                 }

                 completion:nil];

 

        }

        completion:nil];

 

如果你正在使用begin/commit 方法创建动画,nesting works几乎使用相同的方法。Each successive(连续地,继承地)call to beginAnimations:context: within an already open animation block creates a new nested animation block that you can configure as needed。所有的animation blocks必须被commitAnimations关闭在动画被提交和执行之前。

 

6,Implementing Animations That Reverse Themselves

当使用repeat count创建可逆的(reversible)动画时,考虑指定一个非整型值给repeat count。对于一个autoreversing animation, each complete cycle of the animation involves animating from the original value to the new value and back again.If you want your animation to end on the new value,adding 0.5 to the repeat count causes the animation to complete the extra half cycle needed to end at the new value.If you do not include this half step,your animation will animate to the original value and then snap quickly to the new value,which may not be the visual effect you want.

就是repeatCount设置为n.5。

(三)Creating Animated Transitions Between Views

1)Change the visible subviews of an existing view.

2) Replace one view in your view hierarchy with a different view.

 

1,Changing the subviews of a view:

更改view的subviews允许你make moderate(稳健的) changes to the view.例如,你可能添加或删除subviews来切换superview的不同状态。动画完成时,显示相同的view,但是他的内容发生变化了。

在iOS4以后,你使用transitionWithView:duration:options:animations:completion:方法来初始化view的转换动画。如果你需要动画其他更改,你可以包括 UIViewAnimationOptionAllowAnimatedContent选项when calling the method。包括那个选项阻止viewfrom creating snapshots and animates all changes directly。

 

下面的代码例子 如何使用transition animation来使得一个新的text entry page 被添加。在这个例子中,main view有连个不同的text views。一个一直显示,另一个一直隐藏。当用户点击按钮来创建一个新页时,方法切换两个view的显示状态,结果在一个新的空白页中的一个空白的text view准备好接受text。Transition完毕后,the view保存旧页中的内容并且重置现在隐藏的text view以便将来再次使用。

 

- (IBAction)displayNewPage:(id)sender

{

    [UIView transitionWithView:self.view

        duration:1.0

        options:UIViewAnimationOptionTransitionCurlUp

        animations:^{

            currentTextView.hidden = YES;

            swapTextView.hidden = NO;

        }

        completion:^(BOOL finished){

            // Save the old text and then swap the views.

            [self saveNotes:temp];

 

            UIView*    temp = currentTextView;

            currentTextView = swapTextView;

            swapTextView = temp;

        }];

}

 

如果你需要在iOS 3.2之前的版本执行transitions,你可以使用 setAnimationTransition:forView:cache: 方法来指定transition的参数。下面的代码显示了你需要创建的基本动画结构。

 

    [UIView beginAnimations:@"ToggleSiblings" context:nil];

    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];

    [UIView setAnimationDuration:1.0];

 

    // Make your changes

 

    [UIView commitAnimations];

 

 

2,Replacing a View With a Different View:

iOS4以后使用 transitionFromView:toView:duration:options:completion: 方法。方法实际上移除掉firstView并加入另一个view。因此你需要保留一个firstview的reference,如果你希望在以后使用他。如果你想隐藏views代替移除出view结构,传递 UIViewAnimationOptionShowHideTransitionViews key as one of the options.

 

- (IBAction)toggleMainViews:(id)sender {

    [UIView transitionFromView:(displayingPrimary ? primaryView : secondaryView)

        toView:(displayingPrimary ? secondaryView : primaryView)

        duration:1.0

        options:(displayingPrimary ? UIViewAnimationOptionTransitionFlipFromRight :

                    UIViewAnimationOptionTransitionFlipFromLeft)

        completion:^(BOOL finished) {

            if (finished) {

                displayingPrimary = !displayingPrimary;

            }

    }];

}


(四)Linking Multiple Animations Together:

1)如果你使用block-based animations,使用completion handler

2)如果你使用begin/commit animations,使用delegate object

 

或者使用nested animations with different display factors .

 

(五)Animating View and Layer Changes Together

应用程序可以自由地混合view-based和layer-based动画代码,但是配置你的动画参数的过程取决于谁拥有layer。更改一个view-owned layer和更改view本身一样。与你自己创建的layer不同。自定义layer对象忽略view-based动画块参数,并且使用默认的Core Animation 参数。

如果你想为你创建的layer自定义动画参数,你必须使用Core Animation。经典地,animating layers using Core Animation involves creating a CABasicAnimation object or some other concrete(混凝土的,具体的,有形的) subclass of CAAnimation. You then add animation to the corresponding layer. You can apply the animation from either inside or outside a view-based animation block。

 

示例代码:

[UIView animateWithDuration:1.0

    delay:0.0

    options: UIViewAnimationOptionCurveLinear

    animations:^{

        // Animate the first half of the view rotation.

        CGAffineTransform  xform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-180));

        backingView.transform = xform;

 

        // Rotate the embedded CALayer in the opposite direction.

        CABasicAnimation*    layerAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

        layerAnimation.duration = 2.0;

        layerAnimation.beginTime = 0; //CACurrentMediaTime() + 1;

        layerAnimation.valueFunction = [CAValueFunction functionWithName:kCAValueFunctionRotateZ];

        layerAnimation.timingFunction = [CAMediaTimingFunction

                        functionWithName:kCAMediaTimingFunctionLinear];

        layerAnimation.fromValue = [NSNumber numberWithFloat:0.0];

        layerAnimation.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(360.0)];

        layerAnimation.byValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(180.0)];

        [manLayer addAnimation:layerAnimation forKey:@"layerAnimation"];

    }

    completion:^(BOOL finished){

        // Now do the second half of the view rotation.

        [UIView animateWithDuration:1.0

             delay: 0.0

             options: UIViewAnimationOptionCurveLinear

             animations:^{

                 CGAffineTransform  xform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-359));

                 backingView.transform = xform;

             }

             completion:^(BOOL finished){

                 backingView.transform = CGAffineTransformIdentity;

         }];

}];

 

if precise(精确的) timing(定时,时间选择,调速) between your view and layer based animations is required,推荐 you create all of the animations using Core Animation. 你可能发现一些动画使用Core Animation更容易实现。例如,the view-based rotation (上面的例子) requires a multistep sequence for rotations of more than 180 degrees,但在Core Animation部分使用了一个rotation value function that rotates from start to finish through a middle value.

HttpClient跳过HTTPS证书验证的3种实战方法(含完整代码示例) 本文详细介绍了HttpClient跳过HTTPS证书验证的3种实战方法,包括自定义TrustManager、SSLContext配置和HttpClientBuilder高级定制,并提供完整代码示例。这些方法适用于开发和测试环境,帮助开发者快速解决自签名证书或未认证证书导致的HTTPS请求失败问题,但需注意生产环境的安全风险。 阅读详情

相关推荐

计组头哥实验 实验三 MIPS寄存器实验

了解 MIPS 寄存器文件基本概念,进一步熟悉多路选择器、译码器、解复用器等 Logisim 组件的使用,并利用相关组件构建 MIPS 寄存器文件。

gdz0007的博客 1万+

Creating Custom Views//制作自定义的视图

Creating Custom Views//制作自定义的视图 The Android framework has a large set of View classes for interacting with the user and displaying various types of data. But sometimes your app has unique needs tha

qyqingyan的专栏 1116

硅光芯片边耦合器综述(一)

摘要:硅光子学在过去几十年间吸引了越来越多的关注,由于其具有诸多优点,如极低的成本、硅的高折射率带来的高集成密度,以及与现有半导体制造工艺的兼容性,被认为是未来日常应用中的一项充满前景的关键技术。在硅光子集成电路中,光互连是一个重要的问题,而在数据中心和光传输系统等应用场景中,光纤到芯片的光互连至关重要。光纤到芯片的光学耦合主要分为两大类:面外耦合(off-plane coupling)和面内耦合(in-plane coupling)。光栅耦合器属于前者,而边耦合器则属于面内耦合。本文主要关注硅光子集成电路

jhbfsoagoodgio的博客 5055

View Programming Guide for iOS 摘要

View Programming Guide for iOS 摘要文章分类:移动开发原文地址: http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/Introduction/Introduction.html%23//apple_ref/doc/uid/T

jjjsss82的专栏 1012

利用VS2019编写安卓APP的步骤和所遇问题的解决办法

遇到了代码错误,这个可以将鼠标箭头置于错误代码处,这样VS2019给你解决办法,给的方法不能解决,并且读了程序都没有问题,那么你应该重启软件,这样还是不能解决,你尝试重新生成解决方案,还是不能解决有可能是运用的代码与当前版本不相符和、过时了,这个时候应当运用相应版本的代码。注:VS2019编写APP的空间不是很稳定,当你写的代码没有问题,但是出现了报错,可能是空间崩溃了,这个时候可以重启软件。点击“+”号,创建密钥存储,输入“别名”、“密码”、以及下面中的任意一项,直至红色框消失后。

youbestcando的博客 7824

别再死磕Excel了!用Vensim PLE免费版,30分钟搞定你的第一个系统动力学模型

本文介绍如何使用Vensim PLE免费版在30分钟内构建第一个系统动力学模型,无需编程或数学基础。通过个人储蓄增长案例,详细讲解因果回路图绘制、存量流量图创建及方程编写,帮助读者掌握动态系统建模与仿真技巧,适用于理财、项目管理等多种场景。

weixin_29045001的博客 340

View Controller Programming Guid for iOS 笔记

1.View Controller 根蒂根基 1.1 View Controller 分类 ViewController分为container view controller 和content view controller两种类型。 这两种类型只是用处不合,前者作为一个容器容纳其他的view controller,后者用来显示内容,代码上方差别不大。前者包含UINavigationView

wxwldu的专栏 867

群无人机航迹规划-虚拟管道

图源北理工全权老师团队虚拟管道相关论文,文字为个人理解,以ppt形式分享报告。

coldmood77的博客 670

1102 - iOS◍ 文档: View Programming Guide for iOS -- iOS的视图编程指南 第三章 view

参考链接:https://www.cnblogs.com/patientAndPersist/p/3171311.html 别人翻译得很好了,后面的有一些我是直接复制粘贴的 Views --Because view objects are the main way your application interacts with the user, they have many resp...

qq_30185503的博客 530

【QNX+Android虚拟化方案】125 - 如何创建android-spare镜像

【QNX+Android虚拟化方案】125 - 如何创建android-spare镜像

|~~~热爱生活、努力学习的小伙汁~~~| 1108

游戏大厂状态同步与帧同步技术全解析——让虚拟世界万物协同的“时间齿轮”

本文深入解析了大厂游戏开发中的两种核心同步技术:状态同步和帧同步。状态同步适合大型MMO和FPS游戏,由服务端计算并广播世界状态;帧同步则通过统一操作输入实现MOBA和RTS游戏的绝对公平。文章详细比较了两者的差异、适用场景和底层实现原理,并揭秘了《王者荣耀》《PUBG》等热门游戏的同步机制。同时探讨了本地预测、延迟补偿、防作弊等关键技术,以及未来AI和云游戏带来的同步新挑战,为开发者提供了全面的技术参考和实践指南。

本博客聚焦游戏开发技巧、创业实战心得及大学热门课程干货。这里既有技术深度,也有思维广度,无论你是开发者、创业者还是高校学子,都能在这里找到适合自己的成长之路! 934

view controller programming guide for ios

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/DefiningYourSubclass.html#//apple_ref/doc/uid/TP40007457-CH7-SW1

mandagod的博客 293

STM32(HAL库)驱动st7789LCD屏幕(7引脚240*240)

屏幕厂家提供的都是标准库,网上找了找没看到好用的HAL库屏幕驱动,因此本文对HAL库驱动TFT屏幕进行详细讲解。

weixin_44597885的博客 6080

翻译_iOS视图编程指南(View Programming Guide for iOS)之视图和窗口体系

官方最新:View Programming Guide for iOS 前言 前些日子,我发布一个苹果官方文档的翻译,之后就有不少同学朋友问我:翻译苹果官方文档能做什么,开发过程用到的时候很少,浪费时间,还又没什么用。今天,刚好有时间,就在此申明一下翻译苹果官方文档的实质作用: 首先,翻译官方文档可以提高自身英语阅读能力和理解能力,增大自己的词汇量,良好的英语基础会让工作效率更上一层楼的; 其次,对于iOS开发而言,官方文档可以让你更好地理解每一个技术点实现的基本原理,知其然更要知其所以然,这样对iOS

Jacklin 321

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

《View Programming Guide for iOS》笔记

View and Window Architecture 1 每个View的背后都有一个Layer负责渲染。Layer的渲染一般由Core Animation完成,并最终由更底层的硬件缓冲区保存屏幕上的每个点。 2 从视觉上看,子视图会遮住父视图的一部分或者全部。就是说,子视图的zorder一定比父视图大。 3 父视图的某些改变会影响到子视图,比如 resize,hide,alpha,t

苹果君的工作室 2955

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

1026 - iOS◍ 文档: View Programming Guide for iOS -- iOS的视图编程指南 第一章 window and view 架构

View Programming Guide for iOS(Link) 那就先从阅读文档看起吧,看完这个,就去看UIVIew的API了,然后写一些demo,UIView还没时间看。我也不知道在哪里点开这篇文档的超链接了,哦,是从UIView的API点开的超链接。或者从UIKit的推荐文档来看,哎呀,苹果官网的视屏教程也还没看。 我现在需要看的是线程和动画,好的吧,看完UI...

qq_30185503的博客 613

View Programming Guide for iOS 视图编程指南 - iOS

介绍window 和 view的创建,管理,层级,大小调整,动画。

yofer张耀琦的专栏 2693

View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views

Views Because view objects are the main way your application interacts with the user, they have many responsibilities. Here are just a few: 因为视图对象是应用程序跟用户交互的主要方式,所以它们有很多责任。以下是其中一小部分: L...

416

View Programming Guide for iOS ---- iOS 视图编程指南(一)

Next About Windows and Views 关于窗口和视图 In iOS, you use windows and views to present your application’s content on the screen. Windows do not have any visible content themselves but provide...

227

视图编程指南View Programming Guide for iOS-1

一、 View and Window Architecture 1,View Architecture Fundamentals(视图建筑基础): 一个view对象声明了一个矩形区域并且处理drawing and touch事件。一个view也可以是其他view的父对象并且coordinate(坐标) the placement and sizing of those views。 UIVie

The Life with iOS 2179

View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations

Animations Animations provide fluid visual transitions between different states of your user interface. In iOS, animations are used extensively to reposition views, change their size, rem...

261

翻译_iOS视图编程指南(View Programming Guide for iOS)之介绍

官方最新:View Programming Guide for iOS介绍关于窗口和视图在iOS中,你可以使用窗口和视图将你应用的内容呈现在屏幕上。窗口本身是不具备呈现可视化内容的功能的,但它可以用作装有应用视图的容器。视图可以规定在窗口的某一部分显示特定的内容。例如,你可能需要显示图片、文本、图形或者一些组合的视图。同时,你也可以使用视图去组织和管理其他的视图。概览每一个应用都至少有一个窗口和视图

Jacklin 932

1102 - iOS◍ 文档: View Programming Guide for iOS -- iOS的视图编程指南 第二章 window

Windows -- 简介 --Every iOS application needs at least one window—an instance of the UIWindow class—and some may include more than one window. A window object has several responsibilities: It contai...

qq_30185503的博客 279

1203 - iOS◍ 文档: View Programming Guide for iOS -- iOS的视图编程指南 第四章 Animations

官网链接: https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html#//apple_ref/doc/uid/TP40009503-CH6-SW1 Animations -- 动画 ...

qq_30185503的博客 196

View Programming Guide for iOS ---- iOS 视图编程指南(三)---Windows

Windows Every iOS application needs at least one window—an instance of theUIWindowclass—and some may include more than one window. A window object has several responsibilities: 每个iOS应用...

214

View Programming Guide for iOS ---- iOS 视图编程指南(二)---View and Window Architecture

View and Window Architecture 视图和窗口架构 Views and windows present your application’s user interface and handle the interactions with that interface. UIKit and other system frameworks provide a n...

321

View Programming Guide for iOS 学习笔记_0

最近开始看苹果的官方文档,第一篇看的是这个View Programming Guide for iOS,因为我不爱看官方文档,全都是英语,有点头疼,但是这个东西确实不看不可的,所以我逼着自己把这篇Guide翻译一遍,顺便提高一下自己阅读文档的能力。还没有翻译完,有更新我会贴上来的。因为我的翻译能力有限,所以希望大家多多批评指正。

ShorewB的博客 1236
上一篇: 视图编程指南View Programming Guide for iOS-1
下一篇: iPhone iPad 各种控件默认高度
focusjava
博客等级 码龄14年 127粉丝 528原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址