我知道它太晚了,但也許它可以幫助別人。
CALayer具有呈現()函數
FUNC呈現() 返回表示層的狀態,因爲它當前屏幕上顯示的表示層對象的副本。
表示層有一個框架屬性什麼是CGRect並從這裏容易計算出中點。
演示
的代碼在迅速3
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()
}
}
Perfect-非常感謝! –