我在 UIImageView 中有一个透明的 png (self.myImage
)我想绕其中心点旋转。
代码应该非常简单:
[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, 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)