ios 开发中,一些常用的简单的动画可以用 uivew 自带的动画来是实现
- UIView动画可以设置的动画属性有:
- 大小变化(frame)
- 拉伸变化(bounds)
- 中心位置(center)
- 透明度(alpha)
- 背景颜色(backgroundColor)
- 拉伸内容(contentStretch)
今天就学习下 UIVIew 一些常用的动画
1.
// 翻转的动画
[UIView beginAnimations:@"doflip" context:nil];
//设置时常
[UIView setAnimationDuration:1];
//设置动画淡入淡出
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//设置代理
[UIView setAnimationDelegate:self];
//设置翻转方向
[UIView setAnimationTransition:
UIViewAnimationTransitionFlipFromLeft forView:self.redView cache:YES];
//动画结束
[UIView commitAnimations];
效果:
2. 位移动画
[UIView beginAnimations:@"move" context:nil]; [UIView setAnimationDuration:2]; [UIView setAnimationDelegate:self]; //改变它的frame的x,y的值 self.redView.frame=CGRectMake(100,100, 120,100); [UIView commitAnimations];
效果:
另一种写法:
[UIView animateWithDuration:0.5 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGPoint point = _redView.center;
point.y += 150;
[_redView setCenter:point];
} completion:^(BOOL finished) {
}];
效果:
3.关键帧动画
void (^keyFrameBlock)() = ^(){
// 创建颜色数组
NSArray *arrayColors = @[[UIColor orangeColor],
[UIColor yellowColor],
[UIColor greenColor],
[UIColor blueColor],
[UIColor purpleColor],
[UIColor redColor]];
NSUInteger colorCount = [arrayColors count];
// 循环添加关键帧
for (NSUInteger i = 0; i < colorCount; i++) {
[UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount
relativeDuration:1 / (CGFloat)colorCount
animations:^{
[_redView setBackgroundColor:arrayColors[i]];
}];
}
};
[UIView animateKeyframesWithDuration:5.0
delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeCubic | UIViewAnimationOptionCurveLinear
animations:keyFrameBlock
completion:^(BOOL finished) {
// 动画完成后执行
// code...
}];
效果: