Iphone 旋转到特定角度不起作用

Iphone 旋转到特定角度不起作用,iphone,ios,core-animation,Iphone,Ios,Core Animation,我有一张卡通人脸的图像,它可以像摇头一样从左向右旋转一点(就像我们听音乐时那样),持续5-7秒。因此,我正在做以下工作: -(void)rotation{ CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; [rotate setFromValue:[NSNumber numberWithFloat:0.0]]; [rotate setToValue:[NSNum

我有一张卡通人脸的图像,它可以像摇头一样从左向右旋转一点(就像我们听音乐时那样),持续5-7秒。因此,我正在做以下工作:

-(void)rotation{

CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
[rotate setFromValue:[NSNumber numberWithFloat:0.0]];
[rotate setToValue:[NSNumber numberWithFloat:45]];
[rotate setDuration:5.0];
[rotate setAutoreverses:YES];
[rotate setTimingFunction:[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]];

[[faceImage layer] addAnimation:rotate forKey:@"dangleFaceImage"];

}

但是图像像球一样旋转!我认为我的代码一点也不正确。我怎么做旋转

旋转以弧度为单位,而不是度

[rotate setToValue:[NSNumber numberWithFloat: 45*M_PI/180.]];

您将需要设置层的锚定点。将包含面的层的锚定点放置在中下部底部。然后从该点开始旋转。然后会出现头部从颈部倾斜的情况。

对于左侧

    CGAffineTransform transform = CGAffineTransformRotate(imgBg.transform, M_PI/-360);
    imgBg.transform = transform;
右边

    CGAffineTransform transform = CGAffineTransformRotate(imgBg.transform, M_PI/360);
    imgBg.transform = transform;

尝试使用旋转变换进行旋转。您必须先将图像设置为ImageView。假设它是faceImageView

faceImageView.transform = CGAffineTransformRotate(faceImageView.transform, angle);
它将在CALayer.h中旋转到所需角度:)

anchorPoint-定义层边界矩形的定位点。可动画化

@property CGPoint anchorPoint
假设您有一个calayer作为海岬

headLayer.anchorPoint = CGPointMake(headLayer.bounds.size.width/2,headLayer.bounds.size.height);
这将使锚点降低至中底


现在,如果你应用旋转动画,那么中心将是其中的这一点。

首先理解他的问题的最后一行,然后投票。我没有足够的声誉投票支持你!所以,如果有人投票反对你,我很抱歉。但你明白我的意思了。但是我不明白如何用代码来做。你能举个例子吗?在CALayer.h anchorPoint中-定义层边界矩形的锚定点。可动画化@属性CGPoint anchorPoint让我们假设您有一个calayer作为head Headleyer.anchorPoint=CGPointMake(header.bounds.size.width/2,header.bounds.size.height);这将使锚点降低至中底。现在,如果你应用旋转动画,那么中心就是其中的这一点。在这里你可以调整你的角度,非常感谢。但问题是它在旋转,就像较低的位置相对于较高的位置移动一样。但是我需要移动图像的上部位置相对于图像的下部位置同时移动到一侧,而不是两侧!听说过吗?文档中提到,所有的角度都以弧度表示(如“按“角度”弧度旋转”)