动画期间的UIView中心位置 [英] UIView center position during animation

查看:147
本文介绍了动画期间的UIView中心位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定动画过程中UIVIew的中心位置(视图的中点)是什么.带有UIViewPropertyAnimator的动画是这样的:

I would like to determine, what is the center position of a UIVIew (the middle point of the view) during an animation. An animation with UIViewPropertyAnimator like this:

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属性,那么我只会得到动画的目标位置,而不是真实位置.

If I print the circle.center property during the animation then I got just the destination position of the animation not the real position.

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

此打印后,我在控制台上看到了这一点:

After this print I saw this on the console:

(100.0,100.0)

(100.0, 100.0)

(100.0,100.0)

(100.0, 100.0)

...

如何在动画过程中打印实际位置?

How can I print the real position during the animation?

谢谢.

推荐答案

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

I figured out a possible answer.

CALayer 具有presentation()函数

CALayer has a presentation() function

功能介绍() 返回表示图层对象的副本,该对象表示当前在屏幕上显示的图层的状态.

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

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

the presentation layer has a frame property what is a CGRect and from here easy the calculate the midpoint.

演示

代码

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动画在动画过程中确定中心

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

If you have some simpler or nicer solution, please add below. Thanks!

这篇关于动画期间的UIView中心位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆