Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动画流星旅行从上到下在Swift 3_Swift_Animation_Swift3_Uiimageview_Uiimage - Fatal编程技术网

动画流星旅行从上到下在Swift 3

动画流星旅行从上到下在Swift 3,swift,animation,swift3,uiimageview,uiimage,Swift,Animation,Swift3,Uiimageview,Uiimage,我想要一个红色圆圈的图像从屏幕的顶部移动到底部,就像流星穿过屏幕一样。它确实在移动,但它在毫秒内移动,没有动画。如何让它像有尾巴的流星一样平稳地移动 class ViewController: UIViewController { let meteorImage = UIImageView() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup

我想要一个红色圆圈的图像从屏幕的顶部移动到底部,就像流星穿过屏幕一样。它确实在移动,但它在毫秒内移动,没有动画。如何让它像有尾巴的流星一样平稳地移动

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
        }
    }
}
类ViewController:UIViewController{
让meteorImage=UIImageView()
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
self.meteorImage.backgroundColor=.clear
self.meteorImage.frame=CGRect(x:50,y:0,宽度:50,高度:50)
self.meteorImage.ishiden=false
self.meteoimage.image=#imageLiteral(resourceName:“success”)
view.addSubview(self.meteorImage)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
@按下iAction func按钮(\发送方:UIButton){
让totalHeight=Int(view.frame.height)
var currentHeight=0
当currentHeight
动画必须通过动画调用完成

这是制作动画的简单方法

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上的关键帧动画

考虑使用UIKit Dynamics。