有简单的 iPad 应用程序。视图层次结构是: Window - UIView - UIImageView UIImageView 加载的图像大于屏幕尺寸(即 1280 x 2000 对 768 x 1004)。我需要放大图像的任意矩形部分(将其放在屏幕上),然后在屏幕上居中。目前我正在放大以下方式:
-(IBAction)posIt:(id)sender
{
// rectangle size and origin in image's coordinates. Its received from out of application
CGFloat x = 550.f;
CGFloat y = 1006.f;
CGFloat w = 719.f;
CGFloat h = 850.f;
CGSize frameSize = CGSizeMake(_imgView.image.size.width, _imgView.image.size.height);
_imgView.frame = CGRectMakeWithParts(CGPointMake(0, 0), frameSize);
// Calculate center manually because orientation changing
CGPoint superCenter = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
// delta is the factor between size of image size and device screen
CGFloat delta = 768.f/w;
// resizing UIImageView to fit rectangle on the screen
frameSize = CGSizeMake(_imgView.image.size.width * delta, _imgView.image.size.height * delta);
_imgView.frame = CGRectMakeWithParts(CGPointMake(0, 0), frameSize);
// Here is I'm trying to calculate center of rectangle
_imgView.center = CGPointMake(superCenter.x + (superCenter.x - (w/2)*delta - x * delta), superCenter.y + (superCenter.y - (h/2)*delta - y * delta));
}
UIImageView 的 ContentMode 参数是 AspectFit。我相信要将一个点(UIImageView 的中心)移动到另一个点(为了将矩形移动到 superView 的中心),我将使用以下公式:
newX = oldX + oldX - rectCenterX;
newY = oldY + oldY - rectCenterY;
由于 UIImageView 使用delta因子进行缩放,因此 rectCenterX和rectCenterY应该乘以相同的delta。但我错了。矩形中心从 superView 中心跳出。
是否有某种方法可以正确进行这些计算?我想念什么?
提前致谢。