在公司闲得无聊,就学习一些新的东西。不吸收一些新的知识,必然会被社会淘汰。而自己光看不记得话,总是走神,所以把自己学习的东西记录下来,留给以后查看。今天学习一下CATransform3D和CGAffineTransform.
CGAffineTransform是作用于View的主要为2D变换,CATransform3D主要作用于layer,为3D变换使用,这两种变化可以转换。
//旋转
-(IBAction)doRotate:(id)sender
{
//当你改变过一个view.transform属性或者view.layer.transform的时候需要恢复默认状态的话,要先将它们重置,如下代码
transformItem.transform = CGAffineTransformIdentity;
//锚点的默认值是(0.5,0.5),也就是anchorPoint默认在layer的中心点,旋转、平移、缩放都是根据锚点来的,iOS的坐标原点在左上角
transformItem.layer.anchorPoint = CGPointMake(1,0);
//center属性是用CGPoint表示矩形中心点在其父视图的位置
transformItem.center = CGPointMake(CGRectGetWidth(transformItem.bounds),0.0)
[UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
transformItem.transform = CGAffineTransformRotate(transformItem.transform,DEGREES_TO_RADIANS(-90));
} completion:^(Bool finished){
}];
}
//缩放
-(IBAction)doScale:(id)sender
{
CGAffineTransform tr = CGAffineTransformScale(transformItem.transform,2,2);
CGFloat h = transformItem.frame.size.height;
[transformItem.layer setAnchorPoint:CGPointMake(0,1)];
[UIView animateWithDuration:1 delay:0 option:UIViewAnimationOptionCurveEaseOut animation:^{
transformItem.transform = tr;
}completion:^(Bool finisher){
}];
}
//位移
-(IBAction)doTranslate:(id)sender{
[UIView animationWithDuration:1 delay:0 option:UIViewAnimationOptionCurveEaseOut animation:^{
transformItem.transform = CGAffineTransformTranslate(transformItem.transform,100,0);
}completion:^(Bool finished){
}];
CATransform3D
CATransform3D也是一个矩阵,多了一些切变和z轴的值,这里通过CABasicAnimation来实现
//旋转
-(CAAnimation *)animationRotate
{
CATransform3D rotationTransform = CAtransform3DMakeRotation(M_PI,0,1.0,0.0)
CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
animation.during = 1.5;
animation.autoreverses =No;
animation.cumulative =YES;
animation.repeatCount =FLT_MAX;
animation.beginTime=0.6
animation.delegate =self
return animation
}
-(IBAction)do3dRotate:(id)sender{
CAAnimation *myAnimationRotate = [self animationRotate];
[transformItem.layer addAnimation:myAnimationRotate forkey:ANIM_ROTATE];
}