多个 CGAffineTransform 语句不会给出平滑的结果
Multiple CGAffineTranform statements dont give smooth results
我正在我的 UIView 上应用旋转和缩放。 当我应用这些变换中的一个时,它工作正常但是当我尝试同时应用这两个时,结果有点不太顺利。它显示为重置并开始应用。这是我的代码
在此处输入代码
var location:CGPoint = CGPoint.zero
var rotatingLoaction:CGPoint = CGPoint.zero
var deltaAngle: CGFloat!
func handleRotateAndResizeWithPanGestureRecognizer(_ recognizer: UIPanGestureRecognizer) -> Void
{
location = recognizer.location(in: self.superview)
let sizeCenter = CGPoint(x: (self.frame.origin.x + self.frame.size.width) / 2.0, y: (self.frame.origin.y + self.frame.size.height) / 2.0)
if recognizer.state == .began {
initialDistance = sizeCenter.getDistance(point: location)
deltaAngle = atan2(location.y - sizeCenter.y, location.x - sizeCenter.x) - self.transform.getAngle()
}
else if recognizer.state == .changed
{
let ang = atan2(location.y - sizeCenter.y,
location.x - sizeCenter.x);
let angleDiff = deltaAngle - ang;
self.setNeedsDisplay()
var t = CGAffineTransform.identity
let distance = CGFloat(sizeCenter.getDistance(point: location))
let scale = distance / initialDistance
t = t.rotated(by: -angleDiff)
t = t.scaledBy(x: CGFloat(scale), y: CGFloat(scale))
self.transform = t
self.setNeedsDisplay()
print("\(center ) \(scale)")
}
}
更新这是我的getDistance方法,它是用CGPoint的扩展写的
func getDistance(point: CGPoint) -> CGFloat {
let fx = (point.x - self.x);
let fy = (point.y - self.y);
return sqrt((fx*fx + fy*fy))
}
看起来角度按预期工作,但不是缩放。
我可以看到,每次开始拖动时,都会考虑当前的视角,但不会考虑当前的缩放比例。所以答案在于每次开始拖动时相对于其先前状态缩放视图。
如果没有 运行 示例,我无法对此进行测试,但我认为,如果您进行这个小改动,您可能会得到想要的结果:
if recognizer.state == .began {
initialDistance = sizeCenter.getDistance(point: location) / sqrt(transform.a * transform.a + transform.c * transform.c)
我正在我的 UIView 上应用旋转和缩放。 当我应用这些变换中的一个时,它工作正常但是当我尝试同时应用这两个时,结果有点不太顺利。它显示为重置并开始应用。这是我的代码 在此处输入代码
var location:CGPoint = CGPoint.zero
var rotatingLoaction:CGPoint = CGPoint.zero
var deltaAngle: CGFloat!
func handleRotateAndResizeWithPanGestureRecognizer(_ recognizer: UIPanGestureRecognizer) -> Void
{
location = recognizer.location(in: self.superview)
let sizeCenter = CGPoint(x: (self.frame.origin.x + self.frame.size.width) / 2.0, y: (self.frame.origin.y + self.frame.size.height) / 2.0)
if recognizer.state == .began {
initialDistance = sizeCenter.getDistance(point: location)
deltaAngle = atan2(location.y - sizeCenter.y, location.x - sizeCenter.x) - self.transform.getAngle()
}
else if recognizer.state == .changed
{
let ang = atan2(location.y - sizeCenter.y,
location.x - sizeCenter.x);
let angleDiff = deltaAngle - ang;
self.setNeedsDisplay()
var t = CGAffineTransform.identity
let distance = CGFloat(sizeCenter.getDistance(point: location))
let scale = distance / initialDistance
t = t.rotated(by: -angleDiff)
t = t.scaledBy(x: CGFloat(scale), y: CGFloat(scale))
self.transform = t
self.setNeedsDisplay()
print("\(center ) \(scale)")
}
}
更新这是我的getDistance方法,它是用CGPoint的扩展写的
func getDistance(point: CGPoint) -> CGFloat {
let fx = (point.x - self.x);
let fy = (point.y - self.y);
return sqrt((fx*fx + fy*fy))
}
看起来角度按预期工作,但不是缩放。 我可以看到,每次开始拖动时,都会考虑当前的视角,但不会考虑当前的缩放比例。所以答案在于每次开始拖动时相对于其先前状态缩放视图。
如果没有 运行 示例,我无法对此进行测试,但我认为,如果您进行这个小改动,您可能会得到想要的结果:
if recognizer.state == .began {
initialDistance = sizeCenter.getDistance(point: location) / sqrt(transform.a * transform.a + transform.c * transform.c)