Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.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
Ios 在Swift中链接动画_Ios_Swift_Animation_Recursion_Drawing - Fatal编程技术网

Ios 在Swift中链接动画

Ios 在Swift中链接动画,ios,swift,animation,recursion,drawing,Ios,Swift,Animation,Recursion,Drawing,最近,我做了一些代码来画一张脸。我想给这张脸做动画,让它前后摇晃。目前我有这个代码。它向右旋转一次,然后向左旋转更多,然后旋转到原始位置。然而,如果我想让头无限地前后摇晃(来回旋转浴缸),会怎么样。是否可以使用某种递归函数来实现这一点 @IBAction func shakeHead(_ sender: UITapGestureRecognizer) { UIView.animate( withDuration: 0.5, animations: {

最近,我做了一些代码来画一张脸。我想给这张脸做动画,让它前后摇晃。目前我有这个代码。它向右旋转一次,然后向左旋转更多,然后旋转到原始位置。然而,如果我想让头无限地前后摇晃(来回旋转浴缸),会怎么样。是否可以使用某种递归函数来实现这一点

@IBAction func shakeHead(_ sender: UITapGestureRecognizer) {

    UIView.animate(
        withDuration: 0.5,
        animations: {
            self.faceView.transform = self.faceView.transform.rotated(by: self.shakeAngle)
    },
        completion:{ finished in
            if(finished){
                UIView.animate(
                    withDuration: 0.5,
                    animations: {
                        self.faceView.transform = self.faceView.transform.rotated(by: -(self.shakeAngle)*2)
                },
                    completion:{ finished in
                        if(finished){
                            UIView.animate(
                                withDuration: 0.5,
                                animations: {
                                    self.faceView.transform = self.faceView.transform.rotated(by: self.shakeAngle)
                            },
                                completion: nil
                            )
                        }
                }
                )
            }
    }
    )

}

您可以从最终完成块调用
shakeHead

@IBAction func shakeHead(_ sender: UITapGestureRecognizer) {

    UIView.animate(
        withDuration: 0.5,
        animations: {
            self.faceView.transform = self.faceView.transform.rotated(by: self.shakeAngle)
    },
        completion:{ finished in
            if(finished){
                UIView.animate(
                    withDuration: 0.5,
                    animations: {
                        self.faceView.transform = self.faceView.transform.rotated(by: -(self.shakeAngle)*2)
                },
                    completion:{ finished in
                        if(finished){
                            UIView.animate(
                                withDuration: 0.5,
                                animations: {
                                    self.faceView.transform = self.faceView.transform.rotated(by: self.shakeAngle)
                            },
                                completion: { finished in
                                    shakeHead(sender)
                            }
                            )
                        }
                }
                )
            }
    }
    )
}
尽管这在技术上是一个递归调用,但由于代码的异步性质,这并不是一个问题