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 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. The white dotted lines show the center of the 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With