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. 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 correct speed / time and at right angles, but its position shifts. Here is an example of what is happening:

enter image description here

The gray square just shows the position on the screen. Transparent png (contained in UIImageView) is a different shape. White dashed lines show the center of the UIImageView. The initial position of the image is displayed on the left side of the image, the image after rotation is displayed on the right using the code above (which moves slightly to the right). Black and white circles are in the center of the image file.

Is there something I am missing? As far as I understand, the first line above is not required, because these are the default values. Should I install / uninstall something in the storyboard / programmatically?

+7
ios objective-c iphone uiimageview cgaffinetransform
source share
2 answers

I found out that the code I needed to achieve what I needed was simpler than the one that @Albin Joseph gave, but it pointed me in the right direction. My animation should pick up where it left off and turn it to a new position. It will be animated several times, and sometimes it won’t. So 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"]; 
+5
source share

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 simply change the transformView with a different view or ImageView

+13
source share

All Articles