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

前端之家收集整理的这篇文章主要介绍了ios – 围绕其中心点旋转UIImageView?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在UI ImageView(self.myImage)中有一个透明的png,我想围绕它的中心点旋转.
代码应该非常简单:
  1. [self.myImage.layer setAnchorPoint:CGPointMake(0.5,0.5)];
  2. [UIView animateWithDuration:1.0 animations:^{
  3. [self.myImage setTransform:CGAffineTransformMakeRotation(angle)];
  4. }];

图像以正确的速度/时间和直角旋转,但其位置会发生偏移.这是一个正在发生的事情的例子:

灰色方块只是为了在屏幕上显示位置.透明的png(包含在UIImageView中)是另一个图.白色虚线显示UIImageView的中心.图像的左侧显示图像的原始位置,右侧显示使用上述代码旋转后的图像(向右移动一点).黑色和白色圆圈位于图像文件的中心.

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

解决方法

你只需要尝试这个代码
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. CATransform3D transform = CATransform3DIdentity;
  5. transform.m34 = -1 / 500.0;
  6. transform = CATransform3DRotate(transform,.0 * M_PI_2,1,0);/*Here the angle of transform set (Here angle set as 0)*/
  7. self.transformView.layer.transform = transform;
  8. }
  9.  
  10. - (void)viewWillAppear:(BOOL)animated {
  11. [super viewWillAppear:animated];
  12.  
  13. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  14. animation.fromValue = [NSNumber numberWithFloat:0];
  15. animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
  16. animation.duration = 3.0;
  17. animation.repeatCount = HUGE_VALF;
  18. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  19. [self.discView.layer addAnimation:animation forKey:@"transform.rotation.z"];
  20. }
  21.  
  22. - (void)viewDidDisappear:(BOOL)animated {
  23. [super viewDidDisappear:animated];
  24. [self.discView.layer removeAllAnimations];
  25. }
  26.  
  27. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  28. {
  29. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  30. }

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

猜你在找的iOS相关文章