我尝试旋转其超级视图中的图像视图,以使该图像视图在旋转时始终与超级视图的边界相交而不交叉,并适当调整其大小。我该如何实施?图片视图应该能够绕360˚旋转。
在这里,我使用基于三角形公式的计算,同时考虑了初始图像视图的对角线。
也许我应该考虑旋转后的图像视图的新边框(它的x和y坐标为负,并且变换后的帧尺寸也变大)。
到目前为止没有成功,我的图像视图缩小得太快和太大了。因此,据我了解,我的目标是为CGAffineTransformScale
获得适当的比例因子。也许还有其他方法可以做到这一点。
// set initial values
_planImageView.layer.affineTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
_degrees = 0;
_initialWidth = _planImageView.frame.size.width;
_initialHeight = _planImageView.frame.size.height;
_initialAngle = MathUtils::radiansToDegrees(atan((_initialWidth / 2) / (_initialHeight / 2)));
// rotation routine
- (void)rotatePlanWithDegrees:(double)degrees
{
double deltaDegrees = degrees - _degrees;
_initialAngle -= deltaDegrees;
double newAngle = _initialAngle;
double newWidth = (_initialWidth / 2) * tan(MathUtils::degreesToRadians(newAngle)) * 2;
double newHeight = newWidth * (_initialHeight / _initialWidth);
NSLog(@"DEG %f DELTA %f A %f W %f H %f", degrees, deltaDegrees, newAngle, newWidth, newHeight);
double currentScale = newWidth / _initialWidth;
_planImageView.layer.affineTransform = CGAffineTransformScale(CGAffineTransformIdentity, currentScale, currentScale);
_planImageView.layer.affineTransform = CGAffineTransformRotate(_planImageView.layer.affineTransform, (CGFloat) MathUtils::degreesToRadians(degrees));
_degrees = degrees;
self->_planImageView.center = _center;
// NSLog(@"%@", NSStringFromCGRect(_planImageView.frame));
}
多亏了答案,我改写了例行程序,现在可以了!
- (void)rotatePlanWithDegrees:(double)degrees
{
double newWidth =
_initialWidth * abs(cos(MathUtils::degreesToRadians(degrees))) +
_initialHeight * abs(sin(MathUtils::degreesToRadians(degrees)));
double newHeight =
_initialWidth * abs(sin(MathUtils::degreesToRadians(degrees))) +
_initialHeight * abs(cos(MathUtils::degreesToRadians(degrees)));
CGFloat scale = (CGFloat) MIN(
self.planImageScrollView.frame.size.width / newWidth,
self.planImageScrollView.frame.size.height / newHeight);
CGAffineTransform rotationTransform = CGAffineTransformMakeRotation((CGFloat) MathUtils::degreesToRadians(degrees));
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scale, scale);
_planImageView.layer.affineTransform = CGAffineTransformConcat(rotationTransform, scaleTransform);
self->_planImageView.center = _center;
}
答案 0 :(得分:3)
旋转矩形W
x H
时,边界框的尺寸为W' = W |cos Θ| + H |sin Θ|
,H' = W |sin Θ| + H |cos Θ|
。
如果您需要将其适合在W"
x H"
矩形中,则比例因子是W"/W'
和H"/H'
中的最小值。