How to reset UIImageView transform property
In my project i am doing following activity on UIImageView 1.) rotate it 2.) move it 3.) zoom it. My project also has a button named as "New" which will reset all settings of app.my problem is that if i have rotate the image and click on new button,setting of UIImageVIew is not change. can some one has any idea.Thanks in advance
-(void)rotate:(id)sender {
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform oldTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(oldTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
above code i am using for rotation.开发者_StackOverflow社区
If imageView
is your UIImageView
object, resetting the transform
property is done using,
imageView.transform = CGAffineTransformIdentity;
Additionally, you can shorten the rotate:
method a bit if you are not using lastRotation
anywhere else.
-(void)rotate:(UIRotationGestureRecognizer *)gesture {
UIView * view = gesture.view;
view.transform = CGAffineTransformRotate(view.transform, gesture.rotation);
gesture.rotation = 0;
}
For another practical Swift
example for those who are interested, I was able to build off of Deepak's example for rotating an image, then reversing the rotation by using an extension for UIImageView
extension UIImageView {
func rotateCounterClockwise() {
UIView.animateWithDuration(0.3, animations: {
self.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI))
})
}
func rotateToOriginal() {
UIView.animateWithDuration(0.3, animations: {
self.transform = CGAffineTransformIdentity
})
}
}
精彩评论