简体   繁体   English

围绕其中心点旋转UIImageView?

[英]Rotate UIImageView around its center point?

I have a transparent png inside a UIImageView ( self.myImage ) that I want to rotate around its center point. 我在UIImageView( self.myImage )中有一个透明的png,我想围绕它的中心点旋转。 The code should be pretty simple: 代码应该非常简单:

[self.myImage.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
[UIView animateWithDuration:1.0 animations:^{
    [self.myImage setTransform:CGAffineTransformMakeRotation(angle)];
}];

The image rotates at the right speed/time and at the right angle, but its position gets shifted. 图像以正确的速度/时间和直角旋转,但其位置会发生偏移。 Here's an example of what's happening: 这是一个正在发生的事情的例子:

在此输入图像描述

The gray square is just to show position in the screen. 灰色方块只是为了在屏幕上显示位置。 The transparent png (contained in a UIImageView) is the other figure. 透明的png(包含在UIImageView中)是另一个图。 The white dotted lines show the center of the UIImageView. 白色虚线显示UIImageView的中心。 The left side of the image shows the original position of the image, the right side shows the image after being rotated with the above code (which gets shifted a little down to the right). 图像的左侧显示图像的原始位置,右侧显示使用上述代码旋转后的图像(向右移动一点)。 The black and white circles are in the center of the image file. 黑色和白色圆圈位于图像文件的中心。

Is there something that I'm missing? 有什么东西我不见了吗? As far as I understand, the first line above is not required because those are the defaults. 据我所知,上面的第一行不是必需的,因为这些是默认值。 Do I have to set/unset something in the storyboard/programmatically? 我是否必须在故事板中以编程方式设置/取消设置某些内容?

You just try this code 你只需要尝试这个代码

  - (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);
  }

In this you just change transformView with another view or ImageView 在这里,您只需使用另一个视图或ImageView更改transformView

I found out that the code I need to accomplish what I needed was simpler than the one given by @Albin Joseph, but it did point me to the right direction. 我发现我需要完成我需要的代码比@Albin Joseph给出的代码简单,但它确实指向了正确的方向。 My animation needs to pick up where it left off and rotate to a new position. 我的动画需要从中断的位置继续拾取并旋转到新位置。 Some times it will be animated and sometimes it won't. 有时它会动画,有时它不会。 So hence the code: 因此代码:

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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM