swift - 在修复屏幕坐标和动画上翻译UIImageView

时间:2017-12-03 07:34:04

标签: ios swift animation uiviewanimation translate-animation

我有一个UIImageView对象,它全屏显示在屏幕的中心。我制作了一个动画,可以在相同的代码中执行缩放和翻译。这是源代码:

UIView.animate(withDuration: 1.0, animations: {
        let originalTransform = self.idImageView.transform
        let scaledTransform = originalTransform.scaledBy(x: 0.2, y: 0.2)
        let scaledAndTranslatedTransform = scaledTransform.translatedBy(x: 100, y: 100)
        self.idImageView.transform = scaledAndTranslatedTransform
    }, completion: { (finished) in
        self.animationCompeletedSuccessfully(finished)
    })

我希望我的UIImageView移动到x = 100和y = 100的坐标,但是这个转换相对于它的中心移动100点。我怎样才能在窗口坐标系上翻译它? Apple Documentation of traslatedBy(x: , y:) 我已经阅读过这篇文章,但无法清楚地了解协作系统

1 个答案:

答案 0 :(得分:1)

假设您的imageview中心有xy坐标和widthheight。 您想要的目的地:destinationXdestinationY

您的翻译将是:

  • by x:destinationX - x + width/2
  • by y:destinationY - y + height/2

请记住,如果您进行比例缩放,则widthheight应乘以scaleRatio