iOS的动画有很多种 其中UIView的动画的动画比较简单
先介绍一下UIViewAnimationOptions
UIViewAnimationOptionLayoutSubviews //提交动画的时候布局子控件,表示子控件将和父控件一同动画。
UIViewAnimationOptionAllowUserInteraction //动画时允许用户交流,比如触摸
UIViewAnimationOptionBeginFromCurrentState //从当前状态开始动画
UIViewAnimationOptionRepeat //动画无限重复
UIViewAnimationOptionAutoreverse //执行动画回路,前提是设置动画无限重复
UIViewAnimationOptionOverrideInheritedDuration //忽略外层动画嵌套的执行时间
UIViewAnimationOptionOverrideInheritedCurve //忽略外层动画嵌套的时间变化曲线
UIViewAnimationOptionAllowAnimatedContent //通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照
UIViewAnimationOptionShowHideTransitionViews //用显隐的方式替代添加移除图层的动画效果
UIViewAnimationOptionOverrideInheritedOptions //忽略嵌套继承的选项
//时间函数曲线相关
UIViewAnimationOptionCurveEaseInOut //时间曲线函数,由慢到快
UIViewAnimationOptionCurveEaseIn //时间曲线函数,由慢到特别快
UIViewAnimationOptionCurveEaseOut //时间曲线函数,由快到慢
UIViewAnimationOptionCurveLinear //时间曲线函数,匀速
//转场动画相关的
UIViewAnimationOptionTransitionNone //无转场动画
UIViewAnimationOptionTransitionFlipFromLeft //转场从左翻转
UIViewAnimationOptionTransitionFlipFromRight //转场从右翻转
UIViewAnimationOptionTransitionCurlUp //上卷转场
UIViewAnimationOptionTransitionCurlDown //下卷转场
UIViewAnimationOptionTransitionCrossDissolve //转场交叉消失
UIViewAnimationOptionTransitionFlipFromTop //转场从上翻转
UIViewAnimationOptionTransitionFlipFromBottom //转场从下翻转
简单动画 animateWithDuration
一般用来做view的frame改变使动画流畅
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
[UIView animateWithDuration:0.4 animations:^{
self.animateview.center = CGPointMake(self.view.center.x + 10, self.view.center.y + 10);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
self.animateview.center = self.view.center;
} completion:^(BOOL finished) {
}];
}];
animateWithDuration:delay:options:
比如:UIViewAnimationOptionCurveLinear 是线性运动 而UIViewAnimationOptionCurveEaseIn 由到快
可以写在一起比较一下
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
self.animateview.center = CGPointMake(self.animateview.center.x, self.view.center.y);
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.xx.center = self.view.center;
} completion:^(BOOL finished) {
}];
transitionWithView
一般用来做view显示隐藏效果 option是切换的效果
[UIView transitionWithView:self.animateview duration:0.9 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.animateview.hidden = YES;
} completion:^(BOOL finished) {
}];
[UIView transitionWithView:self.xx duration:0.9 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
self.xx.hidden = YES;
} completion:^(BOOL finished) {
}];
transitionFromView
[UIView transitionFromView:self.topView toView:self.bottomView duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) {
NSInteger topIndex = [self.view.subviews indexOfObject:self.topView];
NSInteger bottomIndex = [self.view.subviews indexOfObject:self.bottomView];
[self.view exchangeSubviewAtIndex:topIndex withSubviewAtIndex:bottomIndex];
}];
usingSpringWithDamping: initialSpringVelocity:
[UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:15 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.animateview.center = self.view.center;
} completion:^(BOOL finished) {
}];
animateKeyframesWithDuration
来自 iOS7 Day-by-Day :: Day 11 :: UIView Key-frame Animations void(^animationBlock)() = ^{
NSArray *rainbowColors = @[[UIColor orangeColor],
[UIColor yellowColor],
[UIColor greenColor],
[UIColor blueColor],
[UIColor purpleColor],
[UIColor redColor]];
NSUInteger colorCount = [rainbowColors count];
for(NSUInteger i=0; i<colorCount; i++) {
[UIView addKeyframeWithRelativeStartTime:i/(CGFloat)colorCount
relativeDuration:1/(CGFloat)colorCount
animations:^{
self.xx.backgroundColor = rainbowColors[i];
}];
}
};
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionCalculationModePaced animations:animationBlock completion:nil];