ios – 围绕其中心点旋转UIImageView?

我在UI ImageView(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的中心.图像的左侧显示图像的原始位置,右侧显示使用上述代码旋转后的图像(向右移动一点).黑色和白色圆圈位于图像文件的中心.

有什么东西我不见了吗?据我所知,上面的第一行不是必需的,因为这些是认值.我是否必须在故事板中以编程方式设置/取消设置某些内容

解决方法

你只需要尝试这个代码
- (void)viewDidLoad
{
[super viewDidLoad];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1 / 500.0;
transform = CATransform3DRotate(transform,.0 * M_PI_2,1,0);/*Here the angle of transform set (Here angle set as 0)*/
self.transformView.layer.transform = transform;
}

 - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0];
animation.tovalue = [NSNumber numberWithFloat:2 * M_PI];
animation.duration = 3.0;
animation.repeatCount = HUGE_VALF;
animation.timingFunction = [camediatimingFunction functionWithName:kcamediatimingFunctionLinear];
[self.discView.layer addAnimation:animation forKey:@"transform.rotation.z"];
 }

 - (void)viewDiddisappear:(BOOL)animated {
[super viewDiddisappear:animated];
[self.discView.layer removeAllAnimations];
}    

- (BOOL)shouldAutorotatetoInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  }

在这里,您只需使用另一个视图或ImageView更改transformView

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...