ios4.0以前使用的设置动画的方法,平时不建议用,偶尔有需要的话可以用
// 开始动画
[UIView beginAnimations:@"fdsf" context:nil];
// 设置动画持续时间
[UIView setAnimationDuration:3];
// 设置动画持续时间和动画内容
[UIView animateWithDuration:2 animations:^{
self.green.center = CGPointMake(100, 100);
}];
// 设置动画持续时间和动画内容以及动画完成后做什么事
[UIView animateWithDuration:3 animations:^{
self.green.center = CGPointMake(100, 100);
} completion:^(BOOL finished) {
NSLog(@"fdsf");
}];
// 设置动画持续时间 推迟几秒开始 选择权 动画内容 动画结束后做什么事
[UIView animateWithDuration:3 delay:2 options:UIViewAnimationOptionRepeat animations:^{
// 动画重复次数
[UIView setAnimationRepeatCount:2];
self.green.center = CGPointMake(100, 100);
} completion:^(BOOL finished) {
NSLog(@"fds");
}];
// 结束动画
[UIView commitAnimations];
以下是经常使用的三种种动画:CA动画. (CA是效果不会改变属性值)
// 属性动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
animation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)];
animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 200)];
animation.duration = 3;
[self.view.layer addAnimation:animation forKey:@"hello"];
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGFloat centerx = self.green.center.x;
CGFloat centery = self.green.center.y;
keyFrameAnimation.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(centerx, centery)],[NSValue valueWithCGPoint:CGPointMake(centerx-10, centery)],[NSValue valueWithCGPoint:CGPointMake(centerx, centery)],[NSValue valueWithCGPoint:CGPointMake(centerx+10, centery)],[NSValue valueWithCGPoint:CGPointMake(centerx, centery)],[NSValue valueWithCGPoint:CGPointMake(centerx-10, centery)],[NSValue valueWithCGPoint:CGPointMake(centerx, centery)], nil];
[self.green.layer addAnimation:keyFrameAnimation forKey:@"fds"];
// 过渡动画 (一般设置一个容器在这个容器内做页面的跳转)
CATransition *transition = [CATransition animation];
transition.type = @"cube";
transition.subtype = kCATransitionFromRight;
transition.duration = 3;
transition.startProgress = 0;
transition.endProgress = 0.8;
[self.red.layer addAnimation:transition forKey:@"fds"];