UIView的动画
普通动画
- 方式一
// 1. 开始动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
// 2.动画代码
CGRect frame = self.animationView.frame;
frame.origin.y -= 50;
self.animationView.frame = frame;
// 3.提交动画
[UIView commitAnimations];
- 方式二
[UIView animateWithDuration:2.0 animations:^{
// 执行动画
CGRect frame = self.animationView.frame;
frame.origin.y -= 50;
self.animationView.frame = frame;
}];
[UIView animateWithDuration:1.0 animations:^{
// 执行动画
CGRect frame = self.animationView.frame;
frame.origin.y -= 50;
self.animationView.frame = frame;
} completion:^(BOOL finished) {
// 动画完成做什么事情
self.animationView.backgroundColor = [UIColor blackColor];
}];
缓动动画
/*
缓动动画
options:(枚举)
UIViewAnimationOptionCurveEaseInOut 动画开始/结束比较缓慢,中间相对较快
UIViewAnimationOptionCurveEaseIn 动画开始比较缓慢
UIViewAnimationOptionCurveEaseOut 动画结束比较缓慢
UIViewAnimationOptionCurveLinear 线性---> 匀速
*/
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect frame = self.animationView.frame;
frame.origin.y += 50;
self.animationView.frame = frame;
} completion:^(BOOL finished) {
self.animationView.backgroundColor = [UIColor greenColor];
}];
缩放动画
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
CGRect frame = self.animationView.frame;
frame.size = CGSizeMake(10, 15);
self.animationView.frame = frame;
} completion:^(BOOL finished) {
NSLog(@"缩放动画完成");
}];
透明动画
[UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.animationView.alpha -= 0.9;
} completion:^(BOOL finished) {
NSLog(@"透明动画完成");
}];
转场动画
/*
参数解析:
duration: 动画的持续时间
view: 需要进行转场动画的视图
options: 转场动画的类型
animations: 将改变视图属性的代码放在这个block中
completion: 动画结束后,会自动调用这个block
*/
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
/*
参数解析:
duration: 动画的持续时间
options: 转场动画的类型
animations: 将改变视图属性的代码放在这个block中
completion: 动画结束后,会自动调用这个block
*/
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
方法调用完毕后,相当于执行了下面两句代码:
// 添加toView到父视图
[fromView.superview addSubview:toView];
// 把fromView从父视图中移除
[fromView.superview removeFromSuperview];
首尾式动画
1、定义:
UIView的另一种实现动画方式。
2、执行方式:
执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视图,为此需要将改变属性的代码放在[UIView beginAnimations:nil context:nil]和[UIView commitAnimations]之间,俗称首尾式动画。
3、常见方法解析:
+ (void)setAnimationDelegate:(id)delegate
设置动画代理对象,当动画开始或者结束时会发消息给代理对象
+ (void)setAnimationWillStartSelector:(SEL)selector
当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDidStopSelector:(SEL)selector
当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDuration:(NSTimeInterval)duration
动画的持续时间,秒为单位
+ (void)setAnimationDelay:(NSTimeInterval)delay
动画延迟delay秒后再开始
+ (void)setAnimationStartDate:(NSDate *)startDate
动画的开始时间,默认为now
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve
动画的节奏控制,具体看下面的”备注”
+ (void)setAnimationRepeatCount:(float)repeatCount
动画的重复次数
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
如果设置为YES,代表动画每次重复执行的效果会跟上一次相反
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache
设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好
UIImageView的帧动画
1、定义:
UIImageView可以让一系列的图片在特定的时间内按顺序显示。
2、属性解析:
animationImages:要显示的图片(一个装着UIImage的NSArray) animationDuration:完整地显示一次animationImages中的所有图片所需的时间 animationRepeatCount:动画的执行次数(默认为0,代表无限循环) 3、方法解析:
- (void)startAnimating; 开始动画
- (void)stopAnimating; 停止动画
- (BOOL)isAnimating; 是否正在运行动画
UIActivityIndicatorView 转轮动画
1、定义:
是一个旋转进度轮,可以用来告知用户有一个操作正在进行中,一般用initWithActivityIndicatorStyle初始化(之前的文章有做介绍)。
2、方法解析:
- (void)startAnimating; 开始动画
- (void)stopAnimating; 停止动画
- (BOOL)isAnimating; 是否正在运行动画
/*
UIActivityIndicatorViewStyle 有3个值可供选择:
UIActivityIndicatorViewStyleWhiteLarge //大型白色指示器
UIActivityIndicatorViewStyleWhite //标准尺寸白色指示器
UIActivityIndicatorViewStyleGray //灰色指示器,用于白色背景
*/
用于UIView动画效果的一些枚举值
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, // do not inherit any options or animation type
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
} NS_ENUM_AVAILABLE_IOS(4_0);