2017-05-02 10 views
0

화면을 가로 질러 여행하는 유성처럼 화면의 맨 위에서 아래로 움직이는 빨간 원이 필요합니다. 움직이지만 애니메이션이없는 밀리 초 안에 움직입니다. 꼬리가있는 유성처럼 부드럽게 움직이게하려면 어떻게해야합니까?Swift 3에서 위에서 아래로 흐르는 유성을 애니메이션으로 표현합니다.

class ViewController: UIViewController { 

    let meteorImage = UIImageView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     self.meteorImage.backgroundColor = .clear 
     self.meteorImage.frame = CGRect(x: 50, y: 0, width: 50, height: 50) 
     self.meteorImage.isHidden = false 
     self.meteorImage.image = #imageLiteral(resourceName: "success") 
     view.addSubview(self.meteorImage) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func buttonPressed(_ sender: UIButton) { 
     let totalHeight = Int(view.frame.height) 
     var currentHeight = 0 

     while currentHeight < totalHeight { 
      self.meteorImage.backgroundColor = .clear 
      self.meteorImage.frame = CGRect(x: 50, y: currentHeight, width: 50, height: 50) 
      self.meteorImage.isHidden = false 
      self.meteorImage.image = #imageLiteral(resourceName: "success") 
      view.addSubview(self.meteorImage) 
      currentHeight += 1 
     } 
    } 
} 
+0

UIKit Dynamics 사용을 고려하십시오. – matt

답변

1

애니메이션은 애니메이션 호출로 완료되어야합니다.

이것은 간단한 애니메이션 작업 방법입니다.

UIView.animate(withDuration: 0.1, animations: { 
    // Apply Changes to frame. 
}) 

도 고려해 대신 while 루프를하고 단순히 유성의 프레임의 최종 위치를 설정하고 당신이 먹고 싶어 시간에 기간을 설정합니다.

let newFrame= CGRect(x: 50, 
        y: currentHeight, 
        width: 50, 
        height: 50) 

UIView.animate(withDuration: 3.0, animations: { 
    // Apply Changes to frame. 
    self.meteorImage.frame = newFrame 
}) 

애니메이션을 좀 더 복잡하게 만들려면 UIView에서 키 프레임 애니메이션을 조사 할 수 있습니다.