gpt4 book ai didi

ios - 当对象最初按其中心旋转时,围绕点旋转对象

转载 作者:行者123 更新时间:2023-11-30 11:02:33 26 4
gpt4 key购买 nike

围绕点旋转很简单:One step affine transform for rotation around a point?

但是,我遇到了一个问题:之前旋转“轨道物体”时不起作用。代码如下:

import UIKit

class ViewController: UIViewController {

var orbitingImageView: UIImageView!
var centerImageView: UIImageView!

let rr: CGFloat = .pi / 4

override func viewDidLoad() {
super.viewDidLoad()

centerImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
centerImageView.center = CGPoint(x: 300, y: 400)
centerImageView.backgroundColor = .red
view.addSubview(centerImageView)

orbitingImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
orbitingImageView.center = CGPoint(x: 500, y: 400)
orbitingImageView.backgroundColor = .blue
view.addSubview(orbitingImageView)

orbitingImageView.transform = orbitingImageView.transform
.rotated(by: rr) //without it works fine

n()
}

func n() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { //little trick to make an animation

let angle: CGFloat = 0.05

let orb = self.orbitingImageView!
let cen = self.centerImageView!
let a = CGPoint(x: cen.center.x - orb.center.x,
y: cen.center.y - orb.center.y)

self.orbitingImageView.transform = self.orbitingImageView.transform
.translatedBy(x: a.x, y: a.y)
.rotated(by: angle)
.translatedBy(x: -a.x, y: -a.y)

self.n()
}
}


}

extension CGAffineTransform { //helper methods

func getScale() -> CGFloat {
return (self.a * self.a + self.c * self.c).squareRoot()
}

func getRotation() -> CGFloat {
return atan2(self.b, self.a)
}

}

现在,我可以像这样修改 n() 函数:

func n() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { //little trick to make an animation

let angle: CGFloat = 0.05

let orb = self.orbitingImageView!
let cen = self.centerImageView!
let a = CGPoint(x: cen.center.x - orb.center.x,
y: cen.center.y - orb.center.y)

self.orbitingImageView.transform = self.orbitingImageView.transform
.rotated(by: -self.rr)
.translatedBy(x: a.x, y: a.y)
.rotated(by: angle)
.translatedBy(x: -a.x, y: -a.y)
.rotated(by: self.rr)

self.n()
}
}

它又起作用了。然而,该方法需要存储初始角度(变量rr)。

如何计算 rr?

这只是我在“真实”项目中使用的代码的简化版本。我可以在屏幕上平移/捏合/旋转多个对象。例如,如果我选择了 2 个对象,则第一个对象(我的手指放在上面)应以其中心旋转,而第二个对象应绕第一个点的中心旋转。然后就是这个问题,因为如果第二个对象之前单独旋转(通过它的中心),我需要这个旋转(这里是 rr )。

最佳答案

终于,我找到了解决办法。我没有计算 rr,而是用 1 步方法替换了围绕点的 3 步旋转:

let tr = CGAffineTransform(
a: cos(angle),
b: sin(angle),
c: -sin(angle),
d: cos(angle),
tx: a.x - a.x * cos(angle) + a.y * sin(angle),
ty: a.y - a.x * sin(angle) - a.y * cos(angle)
)

self.orbitingImageView.transform = self.orbitingImageView.transform
.concatenating(tr)

完整代码:

import UIKit

class ViewController: UIViewController {

var orbitingImageView: UIImageView!
var centerImageView: UIImageView!

let rr: CGFloat = .pi / 4

override func viewDidLoad() {
super.viewDidLoad()

centerImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
centerImageView.center = CGPoint(x: 300, y: 400)
centerImageView.backgroundColor = .red
view.addSubview(centerImageView)

orbitingImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
orbitingImageView.center = CGPoint(x: 500, y: 400)
orbitingImageView.backgroundColor = .blue
view.addSubview(orbitingImageView)


orbitingImageView.transform = orbitingImageView.transform
.rotated(by: rr)
.scaledBy(x: 0.5, y: 0.5) //even works with prescaling

n()
}

func n() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {

let angle: CGFloat = 0.05

let orb = self.orbitingImageView!
let cen = self.centerImageView!

let a = CGPoint(x: cen.center.x - orb.center.x,
y: cen.center.y - orb.center.y)

let tr = CGAffineTransform(
a: cos(angle),
b: sin(angle),
c: -sin(angle),
d: cos(angle),
tx: a.x - a.x * cos(angle) + a.y * sin(angle),
ty: a.y - a.x * sin(angle) - a.y * cos(angle)
)

self.orbitingImageView.transform = self.orbitingImageView.transform
.concatenating(tr)

self.n()
}
}

}

现在它可以与预旋转和预缩放一起使用。

关于ios - 当对象最初按其中心旋转时,围绕点旋转对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53156926/

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