我在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