我想创建一个表示饼图的圆,并且希望使该视图能够用一根手指旋转。我已经创建了一些执行此操作的代码,但是有一个问题。旋转视图一次后,我尝试进行的下一个旋转将“饼图”放置在与开始时相同的位置。换句话说,它在旋转后不存储位置。
下面我将在堆栈中显示我已经跟随着另一篇文章的代码。这段代码是用Swift 3编写的,而我使用的是Swift 4,而先前的版本还有一个功能。
image.transform = CGAffineTransformRotate(tempTransform, deltaAngle)
新版本不再可用,所以我有点困惑。
var xOffSet:CGVector = CGVector(dx: 0, dy: 0)
var yOffSet:CGVector = CGVector(dx: 0, dy: 0)
var origin:CGPoint = CGPoint.zero
var tempTransform=CGAffineTransform()
var startingAngle:CGFloat?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("began")
origin = (touches.first?.location(in: self.view))!
print(origin)
print(image.center.x)
print(image.center.y)
xOffSet = CGVector(dx:(origin.x)-image.center.x, dy:(origin.y) - image.center.y)
startingAngle = atan2(xOffSet.dy,xOffSet.dx)
print(startingAngle)
//save the current transform
tempTransform = image.transform
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
print("move")
let touchPoint = touches.first?.location(in: self.view)
yOffSet = CGVector(dx:touchPoint!.x - image.center.x, dy:touchPoint!.y - image.center.y)
let angle = atan2(yOffSet.dy,yOffSet.dx)
print(angle)
let deltaAngle = angle - startingAngle!
print(deltaAngle)
image.transform = CGAffineTransform(rotationAngle: deltaAngle)
}
任何帮助将不胜感激。谢谢