创建一个方形UIView对象testView_,并在viewDidLoad中添加:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect initialRect = testView_.frame;
NSLog(@"before rotation: w %f h %f x %f y %f", initialRect.size.width, initialRect.size.height, initialRect.origin.x, initialRect.origin.y);
testView_.transform = CGAffineTransformMakeRotation(0.1);
NSLog(@"after rotation: w %f, h %f, x %f, y %f", testView_.frame.size.width, testView_.frame.size.height, testView_.frame.origin.x, testView_.frame.origin.y);
testView_.frame = initialRect;
NSLog(@"reassign: w %f, h %f, x %f, y %f", testView_.frame.size.width, testView_.frame.size.height, testView_.frame.origin.x, testView_.frame.origin.y);
}
Run Code Online (Sandbox Code Playgroud)
我在控制台中收到了这个:
2011-04-27 12:30:32.492 Test[31890:207] before rotation: w 100.000000 h 100.000000 x 20.000000 y 20.000000
2011-04-27 12:30:32.494 Test[31890:207] after rotation: w 109.483757, h …Run Code Online (Sandbox Code Playgroud) 所以我需要对视图应用一些缩放和一些旋转(我使用手势执行此操作),因此对于每个手势,我更新当前的scalling和rotation值,例如:
self.scaleWidth *= gesture.scale; //When I detect an horizontal pinch
self.scaleHeight *= gesture.scale; //When I detect a vertical pinch
self.rotationAngle += gesture.rotationAngle; //When I detect a rotation
Run Code Online (Sandbox Code Playgroud)
然后我执行以下操作来转换视图:
CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity, self.scaleWidth, self.scaleHeight);
self.theSubViewToTransform.transform = CGAffineTransformRotate(transform, self.rotationAngle);
Run Code Online (Sandbox Code Playgroud)
当我只进行缩放时,它工作正常.当我进行缩放然后旋转时,它旋转正常.旋转后,我尝试再次缩放,它不能正常工作:应用缩放比如旋转后完成,使我的图像结构变形.
我以为我做了需要做的事情以避免这种情况:每次从身份变换开始,先缩放然后旋转,但显然我错了......
有人知道我的实施有什么问题吗?
谢谢