【问题标题】:How to get current screen xy position of a CAKeyframeAnimation object?如何获取 CAKeyframeAnimation 对象的当前屏幕 xy 位置?
【发布时间】:2019-02-06 19:12:56
【问题描述】:

在 iOS 12 beta 12 下的 Swift 4.2 中,我想获取我沿 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?

【问题讨论】:

    标签: cakeyframeanimation ios12 swift4.2


    【解决方案1】:

    我发现可以通过查询点关联的active表示层来获取当前点所在位置的边框:

            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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多