我要在UIImageView(self.myImage
)中有一个透明的png ,我想绕其中心点旋转。代码应该非常简单:
[self.myImage.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
[UIView animateWithDuration:1.0 animations:^{
[self.myImage setTransform:CGAffineTransformMakeRotation(angle)];
}];
图像以正确的速度/时间和正确的角度旋转,但是其位置发生了偏移。这是正在发生的事的一个例子:
灰色方块仅用于显示屏幕上的位置。另一个是透明png(包含在UIImageView中)。白色虚线表示UIImageView的中心。图像的左侧显示图像的原始位置,右侧显示使用上述代码旋转后的图像(该图像向右下移一点)。黑白圆圈位于图像文件的中心。
有什么我想念的吗?据我了解,上面的第一行不是必需的,因为它们是默认值。我是否必须以编程方式设置/取消情节提要中的某些内容?
我发现完成我需要的代码比@Albin Joseph给出的代码简单,但这确实为我指明了正确的方向。我的动画需要从停下来的地方拾取并旋转到新位置。有时会设置动画,有时则不会。因此代码如下:
CGFloat duration = animated ? 0.5 : 0.01;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [[self.turnIndicatorImage.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"];
animation.toValue = angle;
animation.duration = duration;
animation.fillMode = kCAFillModeForwards;
animation.repeatCount = 0;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[self.turnIndicatorImage.layer addAnimation:animation forKey:@"transform.rotation.z"];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句