UIView动画相关
UIViewAnimationOptions的枚举类型
typedef enum UIViewAnimationOptions : NSUInteger { UIViewAnimationOptionLayoutSubviews = 1 << 0, //在动画的持续时间里,子视图跟随父视图运动 UIViewAnimationOptionAllowUserInteraction = 1 << 1, //在动画的开始后,允许用户交互
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, //所有视图从当前状态开始运行。 UIViewAnimationOptionRepeat = 1 << 3, //重复执行动画 UIViewAnimationOptionAutoreverse = 1 << 4, //动画运行到结束点后仍然以动画方式回到初始点。需设置重复执行动画 UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, //忽略嵌套动画时间设置。 UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, //忽略嵌套动画速度设置。 UIViewAnimationOptionAllowAnimatedContent = 1 << 7, //动画过程中重绘视图(注意仅仅适用于转场动画)。
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, //视图切换时直接隐藏旧视图,而不是将旧视图从父视图移除(仅适用于转场动画) UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, //不继承父动画设置或动画类型。
//设置动画速度
UIViewAnimationOptionCurveEaseInOut = 0 << 16, //由慢到快 UIViewAnimationOptionCurveEaseIn = 1 << 16, //由慢到特别快 UIViewAnimationOptionCurveEaseOut = 2 << 16, //由快到慢 UIViewAnimationOptionCurveLinear = 3 << 16, //匀速
//转场动画相关 UIViewAnimationOptionTransitionNone = 0 << 20, //无转场动画 UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20, //转场从左翻转 UIViewAnimationOptionTransitionFlipFromRight = 2 << 20, //转场从右翻转 UIViewAnimationOptionTransitionCurlUp = 3 << 20, //上卷转场 UIViewAnimationOptionTransitionCurlDown = 4 << 20, //下卷转场 UIViewAnimationOptionTransitionCrossDissolve = 5 << 20, //转场交叉消失 UIViewAnimationOptionTransitionFlipFromTop = 6 << 20, //转场从上翻转 UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20, //转场从下翻转
//动画帧 UIViewAnimationOptionPreferredFramesPerSecondDefault = 0 << 24, //默认的帧每秒. UIViewAnimationOptionPreferredFramesPerSecond60 = 3 << 24, //60帧每秒的帧速率. UIViewAnimationOptionPreferredFramesPerSecond30 = 7 << 24 //30帧每秒的帧速率. } UIViewAnimationOptions;
简单动画 animateWithDuration
一般用来做view的frame改变使动画流畅
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOLfinished))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:
这里的option参数主要是用来指动画view移动的快慢
比如: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
用于2个view之间的切换从fromView切换到toview
[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:
damping指的是阻尼系数 这个系统在0,1之间 系数越大弹性越小
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];