Core animation 或者叫Quartz Core是一个Objective C类库,位于iOS框架的Media层。
在project中使用Core Animation时,首先要将QuartzCore.framework链接到project中,然后在使用animation的类中引入对应的h文件
#import <QuartzCore/QuartzCore.h>
添加动画:
1.使用UIKit中提供的animation API
UIKit中有一些接口可以创建简单的动画,例如:
CGRect smallRect = CGRectMake(0, 0, 20, 30); CGRect bigRect = CGRectMake(0, 0, 200, 300); UIImageView *imgview = [[UIImageView alloc] initWithFrame:smallRect]; imgview.backgroundColor = [UIColor redColor]; [self.view addSubview:imgview]; [UIView animateWithDuration:2 animations:^{ imgview.frame = bigRect; }]; [UIView animateWithDuration:2 animations:^{ imgview.frame = bigRect; } completion:^(BOOL finished){ if(finished){ imgview.backgroundColor = [UIColor blueColor]; } }]; // set delay time,duration time,animation clock,completion block [UIView animateWithDuration:2 delay:5 options:0 animations:^{ imgview.frame = bigRect; }completion:^(BOOL finished){ if(finished){ imgview.backgroundColor = [UIColor blueColor]; } }];
2.使用Core Animation
使用Core Animation可以创建嵌套的对象,并且可以沿着x、y、z轴对它们旋转rotation、缩放scale和转换transform。使用Core animation,你可以创建动态的用户界面而不用使用更底层的图形API,如OpenGL ES。
相关参考:
隐式动画和显式动画
//============================================
一张图片在一定范围内,水平左右移动:
[self.imageView setImage:[UIImage imageNamed:@"推荐页登录按钮.png"]]; CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; theAnimation.duration=1; theAnimation.repeatCount=INT_MAX; theAnimation.autoreverses=YES; theAnimation.fromValue=[NSNumber numberWithFloat:0]; theAnimation.toValue=[NSNumber numberWithFloat:-60]; [self.imageView.layer addAnimation:theAnimation forKey:@"animateLayer"];
一张图片逆时针旋转
[self.imageView setImage:[UIImage imageNamed:@"推荐页登录按钮.png"]]; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; animation.delegate = self; animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI , 0, 0, 1.0)]; animation.duration = 1; animation.cumulative = YES; animation.repeatCount = INT_MAX; [self.imageView.layer addAnimation:animation forKey:@"animation"];
Core Animation相关动画
Core Animation教学:关键桢动画
Core Animation教学:窗口淡入淡出特效
Core Animation教学:使用Transitions制作带动画效果的向导对话框
[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; [_headerView.activityIndicator stopAnimating]; _headerView.hidden = YES; CGRect tFrame = self.tableView.frame; CGRect hFrame = _headerView.frame; self.tableView.frame = CGRectMake(hFrame.origin.x, hFrame.origin.y, tFrame.size.width, tFrame.size.height); [UIView commitAnimations];