一 动画介绍
在iOS中动画实现技术主要是:Core Animation。 Core Animation负责所有的滚动、旋转、缩小和放大以及所有的iOS动画效果。其中UIKit类通常都有animated:参数部分,它可以允许是否使用动画。
本文将介绍UIView动画的实现方式,有基础方法和block方法。
二 实现方法
1 基础动画
[UIView beginAnimations:nil context:nil]; //动画开始
//Code...
[UIView commitAnimations]; //动画结束
//eg 设置lb向下移动
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3];
CGPoint point = self.lb.center;
point.y += 100;
self.lb.center = point;
[UIView commitAnimations];
2 blcok方法1
[UIView animateWithDuration:3 //动画持续时间
animations:^{
//Code...
}];
//eg 改变lb的透明度
[UIView animateWithDuration:3 animations:^{
self.lb.alpha = 0.5;
}];
3 blcok方法2
UIView animateWithDuration:3 //动画持续时间
animations:^{
//动画执行
} completion:^(BOOL finished) {
//动画完成后执行
}];
//eg 旋转lb第一种方法:
CGAffineTransform endAngel = CGAffineTransformMakeRotation(90.0f*(M_PI/180.0f));
[UIView animateWithDuration:3 animations:^{
self.lb.transform = endAngel;
} completion:^(BOOL finished) {
self.stateLb.text = @"动画完成了";
}];
4 blcok方法3
[UIView animateWithDuration:3 //动画持续时间
delay:1 //动画延迟时间
options:UIViewAnimationOptionCurveEaseIn //设置动画过渡效果
animations:^{
//动画执行
} completion:^(BOOL finished) {
//动画完成后执行
}];
//eg 改变lb的字体大小 和位置
[UIView animateWithDuration:3
delay:1
options:UIViewAnimationOptionCurveEaseInOut //设置动画过渡效果
animations:^{
CGPoint center = self.lb.center;
center.x = 200;
self.lb.center = center;
self.lb.textColor = [UIColor blueColor];
} completion:^(BOOL finished) {
self.stateLb.text = @"改变lb的字体大小 和位置 完成*";
}];
5 blcok方法4
[UIView animateWithDuration:3 //动画持续时间
delay:1 //动画延迟时间
usingSpringWithDamping:1.0 //设置类似弹簧效果
initialSpringVelocity:5.0 //设置初始速度
options:UIViewAnimationOptionCurveLinear //设置动画过渡效果
animations:^{
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:3 delay:1 usingSpringWithDamping:1.0 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGPoint center = self.btn.center;
center.y += 50;
self.btn.center = center;
} completion:^(BOOL finished) {
self.stateLb.text = @"弹簧效果结束";
}];