gpt4 book ai didi

swift - 更改 UIView 旋转将更改其框架大小。旋转后如何保持帧大小?

转载 作者:可可西里 更新时间:2023-11-01 01:08:33 24 4
gpt4 key购买 nike

我正在通过 UIRotationGestureRecognizer 旋转 UIView 但我有一个问题,旋转后的 UIView 的框架大小将在旋转时改变.

targetView = UIView(frame: CGRect(x: self.view.center.x, y: self.view.center.y , width: 100, height: 100))
targetView.backgroundColor = .red
targetView.isUserInteractionEnabled = true
self.view.addSubview(targetView)

targetView.center = self.view.center

let rotate = UIRotationGestureRecognizer(target: self, action: #selector(rotatedView))
targetView.addGestureRecognizer(rotate)

这是旋转函数:

@objc func rotatedView(_ sender: UIRotationGestureRecognizer) {
var originalRotation = CGFloat()
if sender.state == .began {
sender.rotation = lastRotation
originalRotation = sender.rotation

print("TargetView Frame Size: [START ROTATE] ", self.targetView.frame.size)
print("TargetView Bounse Size: [START ROTATE] ", self.targetView.bounds)

} else if sender.state == .changed {
let newRotation = sender.rotation + originalRotation
sender.view?.transform = CGAffineTransform(rotationAngle: newRotation)

print("TargetView Frame Size: [ROTATETING] ", self.targetView.frame.size)
print("TargetView Bounse Size: [ROTATING ROTATE] ", self.targetView.bounds)
}
}

尺寸会改变:

TargetView Frame Size: [START ROTATE]  (100.0, 100.0)
TargetView Frame Size: [ROTATETING] (110.564206726133, 110.564206726133)

如何像Start Rotating一样在旋转后保持frame size?!

编辑:

我有这些手势:旋转、调整大小、移动。

移动运行良好,但旋转正在改变大小。我可以在这里使用边界,但如果我也调整大小,边界就不再有用了。

最佳答案

您需要缩放但未旋转的 View 大小。您可以根据 View 的 bounds.sizetransform 进行计算。

示例

let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 150))

// Scale view by 3X horizontally and by 4X vertically and
// rotate by 45 degrees
view.transform = CGAffineTransform(scaleX: 3, y: 4).rotated(by: 45.0 / 180.0 * .pi)

let transform = view.transform

// Find the angle, print it in degrees
let angle = atan2(-transform.c, transform.a)
print(angle * 180.0 / .pi) // 44.99999999999999

// Find scaleX and scaleY
let scaleX = transform.a * cos(angle) - transform.c * sin(angle)
let scaleY = transform.d * cos(angle) + transform.b * sin(angle)
print(scaleX) // 3.0
print(scaleY) // 4.0

print(view.frame.size) // (530.3300858899106, 707.1067811865476)
print(view.bounds.size) // (100.0, 150.0)

let adjustedSize = CGSize(width: view.bounds.size.width * scaleX, height: view.bounds.size.height * scaleY)

// print non-rotated but scaled size
print(adjustedSize) // (300.0, 600.0)

这是一个方便的扩展,适用于缩放、旋转和平移(移动)的 View :

extension CGAffineTransform {
var angle: CGFloat { return atan2(-self.c, self.a) }

var angleInDegrees: CGFloat { return self.angle * 180 / .pi }

var scaleX: CGFloat {
let angle = self.angle
return self.a * cos(angle) - self.c * sin(angle)
}

var scaleY: CGFloat {
let angle = self.angle
return self.d * cos(angle) + self.b * sin(angle)
}
}

现在,如果你有一个 UIView:

let angle = view.transform.angleInDegrees
let scaleX = view.transform.scaleX
let scaleY = view.transform.scaleY
let adjustedSize = CGSize(width: view.bounds.size.width * scaleX, height: view.bounds.size.height * scaleY)

关于swift - 更改 UIView 旋转将更改其框架大小。旋转后如何保持帧大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51808062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com