在我的项目中,我正在UIImageView上进行以下活动 1.)旋转它 2.)移动它 3.)缩放它。 我的项目还有一个名为“New”的按钮,它将重置app.my的所有设置问题是,如果我旋转图像并单击新按钮,UIImageVIew的设置不会改变。可以有人有任何想法。谢谢提前
-(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];
}
以上我用于轮换的代码。
答案 0 :(得分:12)
如果imageView
是您的UIImageView
对象,则重置transform
属性已完成,
imageView.transform = CGAffineTransformIdentity;
此外,如果您未在其他地方使用rotate:
,则可以稍微缩短lastRotation
方法。
-(void)rotate:(UIRotationGestureRecognizer *)gesture {
UIView * view = gesture.view;
view.transform = CGAffineTransformRotate(view.transform, gesture.rotation);
gesture.rotation = 0;
}
答案 1 :(得分:-1)
对于那些感兴趣的人来说,另一个实用的Swift示例,我能够构建Deepak的旋转图像示例,然后通过使用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
})
}
}