gpt4 book ai didi

ios - 在 Swift 中链接动画

转载 作者:可可西里 更新时间:2023-11-01 01:25:51 25 4
gpt4 key购买 nike

最近,我做了一些画脸的代码。我想让脸动画来回摇晃。目前我有这段代码。它向右旋转一次,然后向左旋转一次,最后回到原来的位置。但是,如果我想让头部来回无限摇晃(来回旋转浴缸)怎么办?是否可以使用某种递归函数来执行此操作?

@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
)
}
}
)
}
}
)

}

最佳答案

您可以从最后的完成 block 调用 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)
}
)
}
}
)
}
}
)
}

虽然这在技术上是递归调用,但由于代码的异步性质,这不是问题。

关于ios - 在 Swift 中链接动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41561044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com