Как получить текущую позицию xy экрана объекта CAKeyframeAnimation? - PullRequest
0 голосов
/ 31 августа 2018

В Swift 4.2 под iOS 12 beta 12 я хотел бы получить текущую позицию точки, которую я анимировал вдоль пути в CAKeyframeAnimation.

Код для анимации такой:

    @IBOutlet weak var dot: UIImageView!

    func animateDot() {        
        // Oval path.
        let ovalWidth = UIScreen.main.bounds.width * 0.7
        let ovalHeight = UIScreen.main.bounds.height * 0.75
        let ovalPath = UIBezierPath(ovalIn: CGRect(x: -ovalWidth / 2 , y: -ovalHeight / 2, width: ovalWidth, height: ovalHeight))
        let orbit = CAKeyframeAnimation(keyPath: "position")
        orbit.path = ovalPath.cgPath
        orbit.duration = CFTimeInterval(2.5)
        orbit.isAdditive = true
        orbit.repeatCount = 6
        orbit.calculationMode = kCAAnimationPaced
        orbit.rotationMode = kCAAnimationRotateAuto

        self.dot.layer.add(orbit, forKey: "orbit")
    }

Мой вопрос: как только он будет запущен и запущен, как я могу запросить анимацию в Swift, чтобы получить текущий экран xy центра точки?

1 Ответ

0 голосов
/ 04 сентября 2018

Я обнаружил, что можно запросить активный слой представления, связанный с точкой, чтобы получить кадр, ограничивающий текущую позицию точки:

        if (self.dot.layer.presentation() != nil)
        {
            let dotLocation = self.dot.layer.presentation()?.frame
            os_log("self.dot.layer.presentation()?.frame.minX %12f", type: .debug, dotLocation!.minX)
            os_log("self.dot.layer.presentation()?.frame.maxX %12f", type: .debug, dotLocation!.maxX)
            os_log("self.dot.layer.presentation()?.frame.minY %12f", type: .debug, dotLocation!.minY)
            os_log("self.dot.layer.presentation()?.frame.maxY %12f", type: .debug, dotLocation!.maxY)

            var dotScreenLocation: CGPoint = CGPoint(x: 0, y: 0)
            dotScreenLocation.x = dotLocation!.minX + ((dotLocation!.maxX - dotLocation!.minX) / 2)
            dotScreenLocation.y = dotLocation!.minY + ((dotLocation!.maxY - dotLocation!.minY) / 2)
            os_log("self.dot.layer.presentation()?.frame center x,y %12f,%12f", type: .debug, dotScreenLocation.x, dotScreenLocation.y)
}
...