您的位置:首页 > 产品设计 > UI/UE

iOS 官方文档 UIView

2015-11-24 17:39 381 查看

一、初始化方法

1、- initWithFrame:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 300)];


二、配置View的视图外观

1、backgroundColor 设置背景颜色

view.backgroundColor = [UIColor orangeColor];


(1) backgroundColor顾名思义就是背景颜色,就是整个UIView子类的充满视图的颜色;

(2) 默认父视图设置的背景颜色,如果子视图的背景颜色也是default那么默认透明的视图都是父视图的背景颜色;

(3) 背景颜色对于视图上的带有线条的内容是无法改变的,比如字体颜色;

2、hidden 是否隐藏

该属性为BOOL值,用来表示UIView是否隐藏,默认值是NO。 当值设为YES时:

1、当前的UIView和subview都会被隐藏,而不管subview的hidden值为多少。

2、当前UIView会从响应者链中移除,而响应者链中的下一个会成为第一响应者 总之,同alpha为0时的显示效果相同。

view.hidden = NO;


3、alpha 透明度设置

液晶显示器是由一个个的像素点组成的,每个像素点都可以显示一个由RGBA颜色空间组成的一种色值。其中的A就表示透明度alpha,UIView中alpha是一个浮点值,取值范围0~1.0,表示从完全透明到完全不透明。

当把alpha的值设置成0以后: 1、当前的UIView和subview都会被隐藏,而不管subview的alpha值为多少。

2、当前UIView会从响应者链中移除,而响应者链中的下一个会成为第一响应者 alpha的默认值是1.0。

另外,更改alpha值时,默认是有动画效果的,这是因为图层在Cocoa中是由Core

Animation中CALayer表示的,该动画效果是CALayer的隐含动画。当然也有办法禁用此动画效果。

view.alpha = 0.5;


4、opaque 是否透明

图层叠加时,设置叠加部分图层显示的透明度。

view.opaque = NO;


5、tintColor

[[UIBarButtonItem  appearance]  setTintColor:[UIColor  blueColor]];


(1) tintColor字面意思也是色彩,痕迹,相当于是一个描述一个视图中的线条的颜色,这与痕迹的中文释义不谋而合;

(2) tintColor是描述线条轮廓的一种颜色,该颜色默认具有传递性,默认状态下最底部的视图的tintcolor会一直往上面的视图传递;

(3) 如果子视图改变了tintcolor那么将会和父视图的tintColor不一样;传递链从此处断开;

6、tintAdjustmentMode

view.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;


7、clipsToBounds

作用:决定了子视图的显示范围。具体的说,就是当取值为YES时,剪裁超出父视图范围的子视图部分;当取值为NO时,不剪裁子视图。默认值为NO。

view.clipsToBounds = YES;


8、clearsContextBeforeDrawing

view.clearsContextBeforeDrawing = YES;


9、maskView

10、+ layerClass

11、layer

三、配置与事件相关的行为

1、userInteractionEnabled

(1) 该属性值为布尔类型,如属性本身的名称所释,该属性决定UIView是否接受并响应用户的交互。

(2) 当值设置为NO后,UIView会忽略那些原本应该发生在其自身的诸如touch和keyboard等用户事件,并将这些事件从消息队列中移除出去。当值设置为YES后,这些用户事件会正常的派发至UIView本身(前提事件确实发生在该view上),UIView会按照之前注册的事件处理方法来响应这些事件。

(3) 在一次动画执行流程中,动画包含的所有UIView都会被临时禁止用户交互,而不管每个UIView本身userInteractionEnabled此时的属性值是YES还是NO。但是在配置动画时,通过添加UIViewAnimationOptionAllowUserInteraction选项可以禁止这种行为的发生,使UIView即使是在执行动画期间依然能响应用户事件。

2、multipleTouchEnabled

multipleTouchEnabled 默认是NO,如果设置为YES则支持多点触碰。

3、exclusiveTouch

(1) 默认是NO,如果设置为YES则当前UIView会独占整个Touch事件。具体来说就是如果UIView设置了exclusiveTouch属性为YES则当这个UIView成为第一响应者时,在手指离开屏幕前其他view不会响应任何touch事件。

(2) 作用举例:UITableView的每个cell都需要使用exclusive,否则同时点击多个cell会触发每个视图的事件响应。手势识别会忽略此属性。

四、配置矩形的边界框

1、frame

描述当前视图在其父视图中的位置和大小

2、bounds

描述当前视图在其自身坐标系统中的位置和大小

3、center

描述当前视图的中心点在其父视图中的位置

4、transform

UIView有个transform的属性,通过设置该属性,我们可以实现调整该view在其superView中的大小和位置,具体来说,Transform(变化矩阵)是一种3×3的矩阵,通过这个矩阵我们可以对一个坐标系统进行缩放,平移,旋转以及这两者的任意组着操作。而且矩阵的操作不具备交换律,即矩阵的操作的顺序不同会导致不同的结果。

常用的三种实现选中的方式:

view.transform=CGAffineTransformScale(view.transform, 0.5, 0.5); // 实现的是放大和缩小 view.transform=CGAffineTransformRotate(view.transform, 0.2); //实现的是旋转 view.transform=CGAffineTransformTranslate(view.transform, 20, 20); //实现的是平移

由此可以发现屏幕旋转其实就是通过view的矩阵变化实现,当设备监测到旋转的时候,会通知当前程序,当前程序再通知程序中的window,window会通知它的rootViewController的,rootViewController对其view的transform进行设置,最终完成旋转。

五、管理View的层次结构

1、superview

接收者的父类。

2、subviews

接收者的子类。

3、window

UIWindow对象是所有UIView的根视图,管理和协调的应用程序的显示、分发事件给View。UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件。

4、- addSubview:

添加一个子视图到接收者并让它在最上面显示出来。

5、- bringSubviewToFront:

将某个子视图调整到最上边。

-(void)bringSubviewToFront:(UIView *)view


6、- sendSubviewToBack:

将某个子视图调整到最下边。

-(void)sendSubviewToBack:(UIView *)view


7、- removeFromSuperview

将子视图从父视图中移除掉。

8、- insertSubview:atIndex:

在特定的位置,插入一个子视图。

view:被插入的子视图。

index:被插入的位置下标,位置下标从0开始;下标不能大于子视图的总数。

-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index


9、- insertSubview:aboveSubview:

插入一个View到指定View的上层。

-(void)insertSubview:(UIView *)view aboveSubview:(UIView
*)siblingSubView


10、- insertSubview:belowSubview:

插入一个View到指定View的下层。

-(void)insertSubview:(UIView *)view belowSubview:(UIView
*)siblingSubView


11、-exchangeSubviewAtIndex:withSubviewAtIndex:

根据下标交换两个层的位置。

(void)exchangeSubviewAtIndex:(NSInteger)index withSubviewAtIndex:(NSInteger)index


12、- isDescendantOfView:

返回一个布尔值指出接收者是否是给定视图的子视图或者指向那个视图.

- (BOOL)isDescendantOfView:(UIView *)view
参数
view
一个视图用来测试子视图在视图层次中的关系
返回值
如果接收者是视图的子视图就返回YES,或者视图就是接收者;否则就是NO


六、配置布局行为

1、autoresizingMask

在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高。

enum {
UIViewAutoresizingNone
= 0,
UIViewAutoresizingFlexibleLeftMargin
= 1 << 0,
UIViewAutoresizingFlexibleWidth
= 1 << 1,
UIViewAutoresizingFlexibleRightMargin
= 1 << 2,
UIViewAutoresizingFlexibleTopMargin
= 1 << 3,
UIViewAutoresizingFlexibleHeight
= 1 << 4,
UIViewAutoresizingFlexibleBottomMargin
= 1 << 5
};


UIViewAutoresizingNone就是不自动调整。

UIViewAutoresizingFlexibleLeftMargin 自动调整与superView左边的距离,保证与superView右边的距离不变。

UIViewAutoresizingFlexibleRightMargin 自动调整与superView的右边距离,保证与superView左边的距离不变。

UIViewAutoresizingFlexibleTopMargin 自动调整与superView顶部的距离,保证与superView底部的距离不变。

UIViewAutoresizingFlexibleBottomMargin 自动调整与superView底部的距离,也就是说,与superView顶部的距离不变。

UIViewAutoresizingFlexibleWidth 自动调整自己的宽度,保证与superView左边和右边的距离不变。

UIViewAutoresizingFlexibleHeight 自动调整自己的高度,保证与superView顶部和底部的距离不变。

UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin 自动调整与superView左边的距离,保证与左边的距离和右边的距离和原来距左边和右边的距离的比例不变。比如原来距离为20,30,调整后的距离应为68,102,即68/20=102/30。

其它的组合类似。

2、autoresizesSubviews

//高度自动伸缩,右边间距自动伸缩

self.autoresizesSubviews = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;


UIViewAutoresizingNone

这个常量如果被设置,视图将不进行自动尺寸调整。

UIViewAutoresizingFlexibleHeight

这个常量如果被设置,视图的高度将和父视图的高度一起成比例变化。否则,视图的高度将保持不变。

UIViewAutoresizingFlexibleWidth

这个常量如果被设置,视图的宽度将和父视图的宽度一起成比例变化。否则,视图的宽度将保持不变。

UIViewAutoresizingFlexibleLeftMargin

这个常量如果被设置,视图的左边界将随着父视图宽度的变化而按比例进行调整。否则,视图和其父视图的左边界的相对位置将保持不变。

UIViewAutoresizingFlexibleRightMargin

这个常量如果被设置,视图的右边界将随着父视图宽度的变化而按比例进行调整。否则,视图和其父视图的右边界的相对位置将保持不变。

UIViewAutoresizingFlexibleBottomMargin

这个常量如果被设置,视图的底边界将随着父视图高度的变化而按比例进行调整。否则,视图和其父视图的底边界的相对位置将保持不变。

UIViewAutoresizingFlexibleTopMargin

这个常量如果被设置,视图的上边界将随着父视图高度的变化而按比例进行调整。否则,视图和其父视图的上边界的相对位置将保持不变。

3、contentMode

UIViewContentMode 都有哪些值:
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
默认值是 0,也就是:
UIViewContentModeScaleToFill


一个个来理解下吧:

UIViewContentModeScaleToFill:表示完全填充在 frame 里。

UIViewContentModeScaleAspectFit:保持比例,都在 frame 内。

UIViewContentModeScaleAspectFill:保持比例,填满但 frame 外也有。

UIViewContentModeRedraw:啥意思我还不懂。

其他的是相似的,好理解:

UIViewContentModeCenter:这个 image 的中心与 frame 的中心重合。

UIViewContentModeTop:这个 image 的上边缘与 frame 的上边缘重合。

UIViewContentModeBottom:这个 image 的下边缘与 frame 的下边缘重合。

UIViewContentModeLeft:这个 image 的左边缘与 frame 的左边缘重合。

UIViewContentModeRight:这个 image 的右边缘与 frame 的右边缘重合。

UIViewContentModeTopLeft:类似。

UIViewContentModeTopRight:类似。

UIViewContentModeBottomLeft:类似。

UIViewContentModeBottomRight:类似。

4、- sizeThatFits:

让视图计算最适合子视图的大小,即能把全部子视图显示出来所需要的最小的size

5、- sizeToFit

根据子视图的大小位置,调整视图,使其恰好围绕子视图,也就是说自动适应子视图的大小,只显示子视图

七、Laying out Subviews

1、- layoutSubviews

2、- setNeedsLayout

3、- layoutIfNeeded

4、+ requiresConstraintBasedLayout

5、translatesAutoresizingMaskIntoConstraints

八、Creating Constraints Using Layout Anchors

1、bottomAnchor

2、centerXAnchor

3、centerYAnchor

4、firstBaselineAnchor

5、heightAnchor

6、lastBaselineAnchor

7、leadingAnchor

8、leftAnchor

9、rightAnchor

10、topAnchor

11、trailingAnchor

12、widthAnchor

九、Managing the View’s Constraints

1、constraints

2、- addConstraint:

3、- addConstraints:

4、- removeConstraint:

5、- removeConstraints:

十、Working with Layout Guides

1、- addLayoutGuide:

2、layoutGuides

3、layoutMarginsGuide

4、readableContentGuide

5、- removeLayoutGuide:

十一、Measuring in Auto Layout

1、- systemLayoutSizeFittingSize:

2、- systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:

3、- intrinsicContentSize

4、- invalidateIntrinsicContentSize

5、- contentCompressionResistancePriorityForAxis:

6、- setContentCompressionResistancePriority:forAxis:

7、- contentHuggingPriorityForAxis:

8、- setContentHuggingPriority:forAxis:

十二、Aligning Views in Auto Layout

1、- alignmentRectForFrame:

2、- frameForAlignmentRect:

3、- alignmentRectInsets

4、- viewForBaselineLayout

5、viewForFirstBaselineLayout

6、viewForLastBaselineLayout

十三、Triggering Auto Layout

1、- needsUpdateConstraints

2、- setNeedsUpdateConstraints

3、- updateConstraints

4、- updateConstraintsIfNeeded

十四、Debugging Auto Layout

1、- constraintsAffectingLayoutForAxis:

2、- hasAmbiguousLayout

3、- exerciseAmbiguityInLayout

十五、Managing the User Interface Direction

1、semanticContentAttribute

2、+ userInterfaceLayoutDirectionForSemanticContentAttribute:

十六、Configuring Content Margins

1、layoutMargins

2、preservesSuperviewLayoutMargins

3、- layoutMarginsDidChange

十七、Drawing and Updating the View

1、- drawRect:

(1) 方法原型

- (void)drawRect:(CGRect)rect


(2) drawRect在以下情况下会被调用:

a、如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。drawRect 掉用是在Controller->loadView, Controller->viewDidLoad 两方法之后掉用的.所以不用担心在 控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些View draw的时候需要用到某些变量 值).

b、该方法在调用sizeToFit后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。

c、通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。

以上1,2推荐;而3,4不提倡

(3) drawRect方法使用注意点:

a、 若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate 的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或 者 setNeedsDisplayInRect,让系统自动调该方法。

b、若使用calayer绘图,只能在drawInContext: 中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法

c、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕

2、- setNeedsDisplay

3、- setNeedsDisplayInRect:

4、contentScaleFactor

5、- tintColorDidChange

十八、Formatting Printed View Content

1、- viewPrintFormatter

2、- drawRect:forViewPrintFormatter:

十九、Managing Gesture Recognizers

1、- addGestureRecognizer:

2、- removeGestureRecognizer:

3、gestureRecognizers

4、- gestureRecognizerShouldBegin:

二十、UIView动画块

在iOS4.0 以后苹果推出了动画块,代替了之前iOS2.0推出的UIView基本动画,苹果更推荐开发者使用动画块。

1、+ animateWithDuration:delay:options:animations:completion:

(1) 方法原型

+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion

//这个动画块方法是最全面的,可以设置动画延迟执行、动画属性、动画速度、动画专场、动画开始执行操作、动画结束执行操作。


(2) 事例代码

- (void)animationBlock1
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, self.view.frame.size.width-200, 200)];
imageView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:imageView];

[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionLayoutSubviews
animations:^{
imageView.backgroundColor = [UIColor redColor];
NSLog(@"动画开始");
}
completion:^(BOOL finished) {
imageView.alpha = 0.5;
NSLog(@"动画结束");
}];
}


(3) option 参数详解 (可以同时选择多个进行设置)

a、常规动画属性设置

UIViewAnimationOptionLayoutSubviews:动画过程中保证子视图跟随运动。

UIViewAnimationOptionAllowUserInteraction:动画过程中允许用户交互。

UIViewAnimationOptionBeginFromCurrentState:所有视图从当前状态开始运行。

UIViewAnimationOptionRepeat:重复运行动画。

UIViewAnimationOptionAutoreverse :动画运行到结束点后仍然以动画方式回到初始点。

UIViewAnimationOptionOverrideInheritedDuration:忽略嵌套动画时间设置。

UIViewAnimationOptionOverrideInheritedCurve:忽略嵌套动画速度设置。

UIViewAnimationOptionAllowAnimatedContent:动画过程中重绘视图(注意仅仅适用于转场动画)。

UIViewAnimationOptionShowHideTransitionViews:视图切换时直接隐藏旧视图、显示新视图,而不是将旧视图从父视图移除(仅仅适用于转场动画)

UIViewAnimationOptionOverrideInheritedOptions :不继承父动画设置或动画类型。

b、动画速度控制(可从其中选择一个设置)

UIViewAnimationOptionCurveEaseInOut:动画先缓慢,然后逐渐加速。

UIViewAnimationOptionCurveEaseIn :动画逐渐变慢。

UIViewAnimationOptionCurveEaseOut:动画逐渐加速。

UIViewAnimationOptionCurveLinear :动画匀速执行,默认值。

c、转场类型(仅适用于转场动画设置,可以从中选择一个进行设置,基本动画、关键帧动画不需要设置)

UIViewAnimationOptionTransitionNone:没有转场动画效果。

UIViewAnimationOptionTransitionFlipFromLeft :从左侧翻转效果。

UIViewAnimationOptionTransitionFlipFromRight:从右侧翻转效果。

UIViewAnimationOptionTransitionCurlUp:向后翻页的动画过渡效果。

UIViewAnimationOptionTransitionCurlDown :向前翻页的动画过渡效果。

UIViewAnimationOptionTransitionCrossDissolve:旧视图溶解消失显示下一个新视图的效果。

UIViewAnimationOptionTransitionFlipFromTop :从上方翻转效果。

UIViewAnimationOptionTransitionFlipFromBottom:从底部翻转效果。

2、+ animateWithDuration:animations:completion:

(1) 方法原型

+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion

//这个动画块方法比较全面,可以设置动画开始执行操作、动画结束执行操作。


(2) 事例代码

- (void)animationBlock2
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, self.view.frame.size.width-200, 200)];
imageView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:imageView];

[UIView animateWithDuration:1.0
animations:^{
imageView.backgroundColor = [UIColor redColor];
NSLog(@"动画开始");
}
completion:^(BOOL finished) {
imageView.alpha = 0.5;
NSLog(@"动画结束");
}];
}


3、+ animateWithDuration:animations:

(1) 方法原型

+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations

//这个动画块方法很简洁,实现普通的动画。


(2) 事例代码

- (void)animationBlock3
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, self.view.frame.size.width-200, 200)];
imageView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:imageView];

[UIView animateWithDuration:3.0
animations:^{
imageView.backgroundColor = [UIColor redColor];
}];
}


4、+ transitionWithView:duration:options:animations:completion:

(1) 方法原型

+ (void)transitionWithView:(UIView *)view
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion

//动画专场过渡效果


(2) 事例代码

- (void)viewDidLoad {
[super viewDidLoad];

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[btn setBackgroundColor:[UIColor redColor]];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(animationBlock4) forControlEvents:UIControlEventTouchUpInside];

}

- (void)animationBlock4
{
[UIView transitionWithView:self.view
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionCurveEaseIn
animations:^{
NSLog(@"动画开始");
}
completion:^(BOOL finished) {
NSLog(@"动画结束");
}];
}


5、+ transitionFromView:toView:duration:options:completion:

(1) 方法原型

+ (void)transitionFromView:(UIView *)fromView
toView:(UIView *)toView
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
completion:(void (^)(BOOL finished))completion
//动画专场过渡效果


(2) 事例代码

- (void)viewDidLoad {
[super viewDidLoad];

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 50, 40)];
[btn setBackgroundColor:[UIColor redColor]];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(animationBlock5) forControlEvents:UIControlEventTouchUpInside];
}

- (void)animationBlock5
{
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view1.backgroundColor = [UIColor redColor];
[self.view addSubview:view1];

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view2.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view2];

[UIView transitionFromView:view1
toView:view2
duration:3.0
options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionCurveEaseIn
completion:^(BOOL finished) {

}];
}


6、+ animateKeyframesWithDuration:delay:options:animations:completion:

(1) 方法原型

+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewKeyframeAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
//关键帧动画


(2) 事例代码

//关键帧动画
- (void)animationBlock6
{
[UIView animateKeyframesWithDuration:6.f
delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeLinear
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0   // 相对于6秒所开始的时间(第0秒开始动画)
relativeDuration:1/3.0 // 相对于6秒动画的持续时间(动画持续2秒)
animations:^{
self.view.backgroundColor = [UIColor redColor];
}];

[UIView addKeyframeWithRelativeStartTime:1/3.0 // 相对于6秒所开始的时间(第2秒开始动画)
relativeDuration:1/3.0 // 相对于6秒动画的持续时间(动画持续2秒)
animations:^{
self.view.backgroundColor = [UIColor yellowColor];
}];
[UIView addKeyframeWithRelativeStartTime:2/3.0 // 相对于6秒所开始的时间(第4秒开始动画)
relativeDuration:1/3.0 // 相对于6秒动画的持续时间(动画持续2秒)
animations:^{
self.view.backgroundColor = [UIColor greenColor];                                                                }];

}
completion:^(BOOL finished) {
[self animationBlock6];
}];
}


(3) 补充说明

关键帧动画animateKeyframesWithDuration方法需要和addKeyframeWithRelativeStartTime:relativeDuration:animations方法一起使用,否则就跟普通动画一样。

7、+ addKeyframeWithRelativeStartTime:relativeDuration:animations:

(1) 方法原型

+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
relativeDuration:(double)frameDuration
animations:(void (^)(void))animations
//指定时间和关键帧动画


8、+ performSystemAnimation:onViews:options:animations:completion:

(1) 方法原型

+ (void)performSystemAnimation:(UISystemAnimation)animation
onViews:(NSArray<__kindof UIView *> *)views
options:(UIViewAnimationOptions)options
animations:(void (^)(void))parallelAnimations
completion:(void (^)(BOOL finished))completion
//系统动画


(2) 在一组视图上执行指定的系统动画,并可以并行自定义的动画。其中parallelAnimations就是与系统动画并行的自定义动画。

9、+ animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:

(1) 方法原型

+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
usingSpringWithDamping:(CGFloat)dampingRatio
initialSpringVelocity:(CGFloat)velocity
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
//弹簧效果动画


(2) 在原来的基础上又增加了两个参数,实现了像弹簧一样的动画;dampingRatio 这个参数是设置弹簧的阻尼比例;velocity 这个是设置弹簧的最初速度。

10、+ performWithoutAnimation:

(1) 方法原型

+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation
//禁用一个视图动画过渡。


(2) 视图转换时不执行动画,参数为block对象,来执行你要执行的代码。

二十一、UIView视图动画

1、+ beginAnimations:context:

[UIView beginAnimations:@"animaton" context:nil]; //动画开始


2、+ commitAnimations

[UIView commitAnimations];//执行动画


3、+ setAnimationStartDate:

[UIView setAnimationStartDate:[NSDate date]]; //设置动画开始的时间


4、+ setAnimationsEnabled:

[UIView setAnimationsEnabled:YES]; //设置是否开启动画效果


5、+ setAnimationDelegate:

[UIView setAnimationDelegate:self]; //设置动画代理对象,当动画开始或者结束时会发消息给代理对象


6、+ setAnimationWillStartSelector:

[UIView setAnimationDelegate:self]; //设置动画代理对象,当动画开始或者结束时会发消息给代理对象

- (void)animationWillStart
{
NSLog(@"动画将要开始");
}


7、+ setAnimationDidStopSelector:

[UIView setAnimationDidStopSelector:@selector(animationDidStop)]; //动画已经结束

- (void)animationDidStop
{
NSLog(@"动画已经结束");
}


8、+ setAnimationDuration:

[UIView setAnimationDuration:1.0f]; //动画的持续时间,秒为单位


9、+ setAnimationDelay:

[UIView setAnimationDelay:2.0f]; //动画延迟执行


10、+ setAnimationCurve:

[UIView setAnimationCurve:UIViewAnimationCurveLinear]; //设置动画显示的方式


11、+ setAnimationRepeatCount:

[UIView setAnimationRepeatCount:1]; //动画重复的次数


12、+ setAnimationRepeatAutoreverses:

[UIView setAnimationRepeatAutoreverses:YES]; //设置动画是否从头开始


13、+ setAnimationBeginsFromCurrentState:

[UIView setAnimationBeginsFromCurrentState:NO]; //设置动画是否从当前状态开始


14、+ setAnimationTransition:forView:cache:

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view cache:NO]; //设置动画的过度效果


15、+ areAnimationsEnabled

[UIView areAnimationsEnabled]; //动画结束返回YES


二十二、Using Motion Effects

1、- addMotionEffect:

2、motionEffects

3、- removeMotionEffect:

二十三、Preserving and Restoring State

1、restorationIdentifier

2、- encodeRestorableStateWithCoder:

3、- decodeRestorableStateWithCoder:

二十四、Capturing a View Snapshot

1、- snapshotViewAfterScreenUpdates:

2、- resizableSnapshotViewFromRect:afterScreenUpdates:withCapInsets:

3、- drawViewHierarchyInRect:afterScreenUpdates:

二十五、Identifying the View at Runtime

1、tag

2、- viewWithTag:

二十六、Converting Between View Coordinate Systems

1、- convertPoint:toView:

2、- convertPoint:fromView:

3、- convertRect:toView:

4、- convertRect:fromView:

二十七、Hit Testing in a View

1、- hitTest:withEvent:

2、- pointInside:withEvent:

二十八、Ending a View Editing Session

1、- endEditing:

二十九、Observing View-Related Changes

1、- didAddSubview:

2、- willRemoveSubview:

3、- willMoveToSuperview:

4、- didMoveToSuperview

5、- willMoveToWindow:

6、- didMoveToWindow

三十、Observing Focus

1、- canBecomeFocused

2、+ inheritedAnimationDuration

3、focused

三十一、Data Types

1、UIViewAnimationOptions

2、UIViewAnimationCurve

3、UIViewContentMode

4、UIViewTintAdjustmentMode

5、UISystemAnimation

6、UIViewAutoresizing

7、UIViewAnimationTransition

8、Constants

9、UIViewKeyframeAnimationOptions

10、UILayoutConstraintAxis

三十二、Fitting Size

1、UIView Intrinsic Metric Constant

2、UISemanticContentAttribute

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息