CATransform3DMakeRotation 旋转,翻转

来源:互联网 发布:三星官网刷机软件 编辑:程序博客网 时间:2024/06/06 19:05

    // Create basic animation to rotate around the Y and Z axes

// CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

// transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];

// transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(DEGREES_TO_RADIANS(0), 0, 1, 1)];

// transformAnimation.duration = 1.5;

// transformAnimation.autoreverses = YES;//栅格化图层

// transformAnimation.repeatCount = HUGE_VALF;

// transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];



CABasicAnimation *translateAnimation = [CABasicAnimationanimationWithKeyPath:@"transform.translation.x"];

translateAnimation.fromValue = [NSValuevalueWithCATransform3D:layer.transform];

translateAnimation.toValue = [NSNumbernumberWithFloat:tx];

translateAnimation.duration =1.5;

translateAnimation.autoreverses =YES;

translateAnimation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

translateAnimation.repeatCount =HUGE_VALF;

[layer addAnimation:translateAnimationforKey:nil];




CATransform3DMakeRotation 旋转,翻转

 
CATransform3D myTransform;
myTransform = CATransform3DMakeRotation(angle, x, y, z);

该CATransform3DMakeRotation函数创建了一个转变,将在三维轴坐标系以任意弧度旋转层。x-y-z轴的有个确定的范围(介于-1 和+1之间) 。相应的坐标轴指定的值告诉系统在该轴上旋转。例如,如果X轴是设置为-1或1 ,该对象将的X轴的方向上旋转,这意味着将把它垂直旋转。把这些值看做是插入在图像每个坐标轴上的秸秆(Think of these values as inserting straws through the image for each axis.)。如果秸秆插过x轴,图像将沿着秸秆垂直旋转。您可以使用坐标轴角度值创建更复杂的旋转。。对于大多数的用途,但是,值介于-1和+1已经足够。

要水平(垂直)旋转45度,您可以使用下面的代码:

myTransform = CATransform3DMakeRotation(0.78, 1.0, 0.0, 0.0);

要在Y轴上旋转相同的值:
myTransform = CATransform3DMakeRotation(0.78, 0.0, 1.0, 0.0);


0.78 ,用在前面的例子,是由角度值经计算转化为弧度值。要把角度值转化为弧度值,可以使用一个简单的公式Mπ/180 。例如, 45π/180 = 45 ( 3.1415 ) / 180 = 0.7853 。如果你打算在你的程序里面一直都用角度值的话,你可以写一个简单的转化方法,以帮助保持您的代码是可以理解的:

double radians(float degrees) {
    return ( degrees * 3.14159265 ) / 180.0;
}

当你创建一个转换的时候,你将要调用这个方法:

myTransform = CATransform3DMakeRotation(radians(45.0), 0.0, 1.0, 0.0);

当变换(transformation)被创建好了以后,应用在你正在操作的层上。CALayer对象提供了一个transform属性来连接转换。层将执行分配给transform属性的转换:

imageView.layer.transform = myTransform;

当对象被显示后,将会显示应用到它的转换效果。在你的代码中,你任然把它当做是个2D对象。但是它根据提供的转换类型来渲染。