2016-10-17 2 views
0

애니메이션 중에 UIVIew (보기의 가운데 지점)의 중심 위치를 결정하고 싶습니다. 이 같은 UIViewPropertyAnimator와 애니메이션 : 나는 애니메이션 중에 circle.center 속성을 인쇄 할 경우애니메이션 중 UIView 가운데 위치

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

는 나는 애니메이션이 아닌 실제 위치의 단지 대상 위치를 얻었다. 내가 콘솔에 이것을보고이 인쇄 후

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)

...

애니메이션 중에 어떻게 실제 위치를 인쇄 할 수 있습니까?

감사합니다.

+0

달성하려는 목표는 무엇입니까? 원을 중심에 추가하려면 다음을 수행하십시오. 'circle.center = self.view.center' –

+0

애니메이션 중에 원의 정확한 위치 (CGPoint)가 무엇인지 알고 싶습니다. . circle.center 위치를 콘솔에 인쇄하면 시작 위치 (0.0)와 끝 위치 (100,100) 만 표시됩니다. –

답변

1

가능한 답변을 찾았습니다.

CALayer 프레젠테이션() 함수 그것을 현재 화면에 표시되는 층의 상태를 나타내는 프리젠 테이션 레이어 객체의 복사본을 반환

FUNC 프레젠테이션()을 갖는다.

프리젠 테이션 레이어에는 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

당신은 몇 가지 간단한 또는 더 좋은 솔루션이있는 경우에서 온, 아래에 추가하십시오. 감사!