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)
}
)
}
}
)
}
}
)
}
尽管这在技术上是一个递归调用,但由于代码的异步性质,这并不是一个问题