ios - 动画期间的 UIView 中心位置

标签 ios swift uiview swift3 uiviewpropertyanimator

我想确定,动画期间 UIVIew( View 的中间点)的中心位置是什么。像这样的带有 UIViewPropertyAnimator 的动画:

let animator = UIViewPropertyAnimator(duration: 10, curve: .easeInOut)
 
var circle : UIView!
circle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0))
circle.layer.cornerRadius = 20.0
circle.backgroundColor = UIColor.blue
self.view.addSubview(circle)

animator.addAnimations {
    circle.center = CGPoint(x: 100,y: 100)
    //or any other point
}
animator.startAnimation()

如果我在动画期间打印 circle.center 属性,那么我得到的只是动画的目标位置,而不是实际位置。

Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true, block: {
[weak self] (Timer) -> Void in
if let circleCenterPosition = self?.circle.center
{
    print(circleCenterPosition)
}
})

打印后我在控制台上看到了这个:

(100.0, 100.0)

(100.0, 100.0)

...

如何打印动画中的真实位置?

谢谢。

最佳答案

我想出了一个可能的答案。

CALayer有一个 presentation() 函数

func presentation() Returns a copy of the presentation layer object that represents the state of the layer as it currently appears onscreen.

表示层有一个框架属性,即 CGRect,从这里可以轻松计算中点。

演示

enter image description here

代码

class ViewController: UIViewController {
        var circle : UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let animator = UIViewPropertyAnimator(duration: 10, curve: .easeInOut)
        circle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0))
        circle.layer.cornerRadius = 20.0
        circle.backgroundColor = UIColor.blue
        self.view.addSubview(circle)
        Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true)
        {
            [weak self] (myTimer) -> Void in
            if let movingBlueDotFrame = self?.circle.layer.presentation()?.frame
            {
                let blueDotOriginPoint = movingBlueDotFrame.origin
                let blueDotMiddlePoint = CGPoint(x: blueDotOriginPoint.x + movingBlueDotFrame.width/2, y: blueDotOriginPoint.y + movingBlueDotFrame.height/2)
                print(blueDotMiddlePoint)
                if blueDotMiddlePoint == CGPoint(x:100,y:100){
                    print("Yeeaahh")
                    myTimer.invalidate()
                }
            }
       }
        animator.addAnimations {
            self.circle.center = CGPoint(x: 100,y: 100)
        }
        animator.startAnimation()
    }
}

想法来自这里UIView animation determine center during animation

如果您有更简单或更好的解决方案,请在下面添加。谢谢!

关于ios - 动画期间的 UIView 中心位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096501/

相关文章:

ios - UIScrollView 如何更改框架而不更改边界? UIScrollView 是如何工作的?

ios - 如何使用 Xcode 6.0.1 重定向回 url 方案中的应用程序

ios - 如何在 SwiftUI 中更改 ProgressView 口音颜色?

ios - IBInspectable setTitle :forState on UIButton not working

ios - 如何使用带有可选 UIImage 的绑定(bind)?

android - TLS Client Hello 请求在 iOS 或 Android 中包含未知版本

ios - 清除一个区域,以便后面的 View 显示出来

ios - 当用户点击 View 外的任何地方时关闭 View

ios - 提交到应用商店时可以设置多低的部署目标是否有限制?

objective-c - 一个线程与它的 NSAutorelease 池有什么关系?