# iOS学习之UIView Animation

UIView中哪些属性可以做动画？
UIKit和Core Animation都提供了动画支持，但动画支持的层面因两者的技术差异而不同。在UIKit中，动画的执行者是视图对象。Views

Table 4-1 Animatable UIView properties
Property

Changes you can make

> frame 大小重点内容
Modify this property to change the view’s size and position
relative to its superview’s coordinate system. (If the transform
property does not contain the identity transform, modify the bounds

*bounds 边框*
Modify this property to change the view’s size.

*center 中心*
Modify this property to change the view’s position
relative to its superview’s coordinate system.

*transform 转换*
Modify this property to scale, rotate, or translate the
view relative to its center point. Transformations using this
property are always performed in 2D space. (To perform 3D
transformations, you must animate the view’s layer object using Core
Animation.)

*alpha 透明度*
Modify this property to gradually change the transparency
of the view.

*backgroundColor 背景色*
Modify this property to change the view’s   background color.

*contentStretch 内容压缩*

Modify this property to change the way the view’s contents are
stretched to fill the available space.


animateWithDuration:animations:
animateWithDuration:animations:completion:
animateWithDuration:delay:options:animations:completion:

The delay to use before starting the animation
delay设置动画延迟调用时间
The type of timing curve to use during the animation

The number of times the animation should repeat

Whether the animation should reverse itself automatically when it reaches the end

Whether touch events are delivered to views while the animations are in progress

Whether the animation should interrupt any in-progress animations or wait until those are complete before starting

[UIView beginAnimations:@”animation” context:nil];
//添加动画块，在这区间
[UIView commitAnimations];
PS:Changing the value of a property while an animation involving that property is in progress does not stop the current animation. Instead, the animation continues and animates to the new value you just assigned to the property.在动画过程中改变动画中的属性不会停止当的的动画，相反，动画会继续执行并且会执行到最新的属性值

Table 4-2 Methods for configuring animation blocks
Method

Usage

setAnimationStartDate:

setAnimationDelay:

Use either of these methods to specify when the executions should begin executing. If the specified start date is in the past (or the delay is 0), the animations begin as soon as possible.

setAnimationDuration:

Use this method to set the period of time over which to execute the animations.

setAnimationCurve:

Use this method to set the timing curve of the animations. This controls whether animations execute linearly or change speed at certain times.

setAnimationRepeatCount:

setAnimationRepeatAutoreverses:

Use these methods to set the number of times the animation repeats and whether the animation runs in reverse at the end of each complete cycle. For more information about using these methods, see Implementing Animations That Reverse Themselves.

setAnimationDelegate:

setAnimationWillStartSelector:

setAnimationDidStopSelector:

Use these methods to execute code immediately before or after the animations. For more information about using a delegate, see Configuring an Animation Delegate.

setAnimationBeginsFromCurrentState:

Use this method to stop all previous animations immediately and start the new animations from the stopping point. If you pass NO to this method, instead of YES, the new animations do not begin executing until the previous animations stop.

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

[UIView beginAnimations:@"ShowHideView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(showHideDidStop:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_thirdView cache:YES];
// 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];
//    UIView setAnimationTransition:<#(UIViewAnimationTransition)#> forView:<#(UIView *)#> cache:<#(BOOL)#>
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:_thirdView cache:YES];
_thirdView.alpha = 1.0;

[UIView commitAnimations];
}

ps:
Although animation delegates can be used in the block-based methods, there is generally no need to use them there. Instead, place any code you want to run before the animations at the beginning of your block and place any code you want to run after the animations finish in a completion handler.

  [UIView animateWithDuration:2 animations:^{
self.view.alpha=1;
self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionTransitionCurlDown animations:^{
self.view.alpha=.3;
self.view.backgroundColor=[UIColor yellowColor];
} completion:^(BOOL finished) {

}];

} completion:^(BOOL finished) {

}];

View transitions help you hide sudden changes associated with adding, removing, hiding, or showing views in your view hierarchy. You use view transitions to implement the following types of changes:

Change the visible subviews of an existing view. You typically choose this option when you want to make relatively small changes to an existing view.

Replace one view in your view hierarchy with a different view. You typically choose this option when you want to replace a view hierarchy that spans all or most of the screen.
Important: View transitions should not be confused with transitions initiated by view controllers, such as the presentation of modal view controllers or the pushing of new view controllers onto a navigation stack. View transitions affect the view hierarchy only, whereas view-controller transitions change the active view controller as well. Thus, for view transitions, the view controller that was active when you initiated the transition remains active when the transition finishes.

 [UIView transitionWithView:_secondView duration:1.0 options: UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionOverrideInheritedCurve
animations:^{
_secondView.hidden=NO;
} completion:nil];

 [UIView transitionFromView:_thirdView toView: _secondView duration:2 options:UIViewAnimationOptionTransitionFlipFromTop|UIViewAnimationOptionShowHideTransitionViews
completion:^(BOOL finished) { }];

• 本文已收录于以下专栏：

举报原因： 您举报文章：iOS学习之UIView Animation 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)