//控件绕着Z轴旋转(也可以绕着X,Y轴旋转)
CABasicAnimation *rotationAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue=@(M_PI*2.0);
rotationAnimation.duration=1;
rotationAnimation.cumulative=YES;
rotationAnimation.repeatCount=MAXFLOAT;//一直旋转
[_customView.layeraddAnimation:rotationAnimation forKey:@"rotationAnimation"];
//UIView动画一
[UIViewbeginAnimations:@"animation"context:nil];
[UIViewsetAnimationDuration:1.0];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIViewsetAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished)];
[UIViewcommitAnimations];
//UIView动画二
[UIViewanimateWithDuration:1.0animations:^{
//执行动画
NSLog(@"......");
} completion:^(BOOL finished) {
//动画结束后执行
NSLog(@"......");
}];
//CATransition动画
CATransition *animation=[CATransitionanimation];
animation.duration=1.0f;
animation.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//类型有以下几种(苹果私有API):rippleEffect,cube,oglFlip,pageCurl,pageUnCurl,suckEffect,cameraIrisHollowClose,cameraIrisHollowOpen
animation.type=@"rippleEffect";
animation.subtype=kCATransitionFromLeft;
[self.view.layeraddAnimation:animation forKey:@"animation"];
//CATransaction动画
[CATransaction begin];
[CATransactionsetAnimationDuration:1.0];
[CATransactionsetAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[CATransactionsetCompletionBlock:^{
// handle completion here
}];
[CATransaction commit];
//CATransform3D动画(常用在以下函数中,在cell滑动时有酷炫效果)
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
// 1. 配置CATransform3D的内容
CATransform3D transform;
transform = CATransform3DMakeRotation( (90.0*M_PI)/180,0.0, 0.7, 0.4);
transform.m34 = 1.0/ -600;
// 2. 定义cell的初始状态
cell.layer.shadowColor = [[UIColor blackColor] CGColor];
cell.layer.shadowOffset =CGSizeMake(10,10);
cell.alpha = 0;
cell.layer.transform = transform;
cell.layer.anchorPoint =CGPointMake(0,0.5);
// 3. 定义cell的最终状态,并提交动画
[UIView beginAnimations:@"transform"context:NULL];
[UIView setAnimationDuration:0.5];
cell.layer.transform =CATransform3DIdentity;
cell.alpha = 1;
cell.layer.shadowOffset =CGSizeMake(0,0);
cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
[UIView commitAnimations];
//CATransform3D用例1 scale
CABasicAnimation *basicAnim1 = [CABasicAnimation animationWithKeyPath:@"transform"];
basicAnim1.fromValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];
basicAnim1.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1.0)];
[basicAnim1 setAutoreverses:YES]; //原路返回执行动画一遍
[basicAnim1 setDuration:0.8];
[basicAnim1 setRepeatCount:MAXFLOAT];
[_customMyButton.layer addAnimation:basicAnim1 forKey:nil];
//CATransform3D用例2 rotate
CABasicAnimation *basicAnim2=[CABasicAnimation animationWithKeyPath:@"transform"];
basicAnim2.fromValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 1, 1, 0)];
basicAnim2.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(-M_PI, 1, 1, 0)];
[basicAnim2 setDuration:1.0];
[basicAnim2 setRepeatCount:MAXFLOAT];
[basicAnim2 setAutoreverses:YES];
[_customMyImage.layer addAnimation:basicAnim2 forKey:nil];
//用例3 scale+rotate+position
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D rotateTransform = CATransform3DMakeRotation(-M_PI, 0, 0, -1);//旋转变换
CATransform3D scaleTransform = CATransform3DMakeScale(3, 3, 3); //伸缩变换
CATransform3D positionTransform = CATransform3DMakeTranslation(0, 0, 0); //平移变换
CATransform3D combinedTransform =CATransform3DConcat(rotateTransform, scaleTransform); //旋转+伸缩-->组合成新的变换
combinedTransform = CATransform3DConcat(combinedTransform, positionTransform); //再combine一次把三个动作连起来
anim.fromValue=[NSValue valueWithCATransform3D:CATransform3DIdentity]; //放在3D坐标系中最正的位置
anim.toValue=[NSValue valueWithCATransform3D:combinedTransform];
anim.duration=2.0f;
anim.repeatCount=MAXFLOAT;
anim.autoreverses=YES;
[_customMyStepper.layer addAnimation:anim forKey:nil];