gpt4 book ai didi

ios - Swift 3 中的 CGRect 以正确的原点旋转

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

我正在尝试绕其端点旋转一条线(细矩形)。我让它工作正常,但步骤 2 是缩短线并让它仍然围绕端点(旋转中心)旋转。

    // Create and add a colored square
var rod = UIView()
var countsteps = 0
var Mass = 1
var Radius = 1.00
let rectWidth = screenWidth * 0.5
let rectVertical = screenHeight * 0.5
let rectLeft = screenWidth * 0.5

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
// set background color to blue
rod.backgroundColor = UIColor(red: 145/255, green: 170/255, blue: 157/255, alpha: 1)
//rod Line
rod.frame = CGRect(x: rectLeft, y: rectVertical, width: 3, height: rectWidth)
// finally, add the square to the screen
self.view.addSubview(rod)

//Create Timer
_ = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(Acceleration.update), userInfo: nil, repeats: true)
}


func update() {
UIView.animate(withDuration: 0.1, animations: {

//Slope Line
let rectSize = CGFloat(self.Radius) * self.rectWidth / 100
let amountRotation = CGFloat(CGFloat(self.countsteps)/57.3)
let originPoint = CGPoint(x: 0,y: 0)
self.rod.layer.anchorPoint = originPoint
self.rod.transform = CGAffineTransform(rotationAngle: amountRotation)
self.countsteps = self.countsteps + 1
if (self.countsteps > 359) {
self.countsteps = 0
}
})
}

我尝试添加这一行:

self.rod.frame.size = CGSize(width: 3.00, height: rectSize)

但是,这会使盒子旋转,同时改变围绕不同轴的比例。

最佳答案

尝试添加这一行:

self.rod.layer.position = originPoint

所以,你的更新函数看起来像:

    func update() {
UIView.animate(withDuration: 0.1, animations: {

//Slope Line
let rectSize = CGFloat(self.Radius) * self.rectWidth / 100
let amountRotation = CGFloat(CGFloat(self.countsteps)/57.3)
let originPoint = CGPoint(x: 0,y: 0)
self.rod.layer.anchorPoint = originPoint
self.rod.layer.position = originPoint
self.rod.transform = CGAffineTransform(rotationAngle: amountRotation)
self.countsteps = self.countsteps + 1
if (self.countsteps > 359) {
self.countsteps = 0
}
})
}

我强烈建议向图层和 View 背景添加一些颜色,使用一些 Alpha,它对于检测系统正在做什么非常有用,就像可视化调试一样。

关于ios - Swift 3 中的 CGRect 以正确的原点旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780071/

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