UIView 动画选项重复计数
UIView Animation Options Repeat count
我的 Swift 代码有一些问题,我试图让 UIImageView 对象淡出并重新出现一次,但在让动画只播放一次方面遇到了一些问题。
@IBOutlet weak var ball: UIImageView!
@IBAction func onFadeClick(_ sender: Any) {
UIView.animate(withDuration: 1, delay: 0.0, options: [.repeat, .autoreverse], animations: {
self.ball.alpha = 0.0
}, completion: nil)
}
我试图阅读文档和之前的问题,但都提到使用 setAnimationRepeatCount,但 xcode 有一个错误,指出它在 iOS13 中已贬值(这也不起作用) .播放一次后是否有任何内置函数可以用来停止动画?我在某处读到说使用回调函数并重新初始化动画,但我不确定该怎么做。还是使用 UIViewPropertyAnimator 而不是 UIView.animate 更好?
我刚刚开始学习Swift,非常感谢任何帮助或指导!
您可以使用以下方法来实现您的目标:
UIView.animateKeyframes(withDuration: 2, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
self.ball.alpha = 0.0
}
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
self.ball.alpha = 1
}
}) { (isFinished) in
}
iOS 13 已弃用方法的替代方法是 https://developer.apple.com/documentation/uikit/uiview/3043564-modifyanimations。示例:
UIView.animate(withDuration:1, animations: {
UIView.modifyAnimations(withRepeatCount: 3, autoreverses: true, animations: {
// whatever
})
})
但是,已弃用的方法仍然有效;它只是被弃用了。
而不是最后一个 }) 使用:
}, completion: { finished in
self.alpha = 1
})
Swift 5.x
当前 iOS 已弃用方法的语法替换可以像下面的示例一样编写:
UIView.animate(withDuration:1,
delay: 0.5,
options: [.curveEaseInOut, .autoreverse, .repeat]) {
UIView.modifyAnimations(withRepeatCount: 3, autoreverses: true) {
print("repeated stuff")
}
} completion: { _ in
print("done")
}
我的 Swift 代码有一些问题,我试图让 UIImageView 对象淡出并重新出现一次,但在让动画只播放一次方面遇到了一些问题。
@IBOutlet weak var ball: UIImageView!
@IBAction func onFadeClick(_ sender: Any) {
UIView.animate(withDuration: 1, delay: 0.0, options: [.repeat, .autoreverse], animations: {
self.ball.alpha = 0.0
}, completion: nil)
}
我试图阅读文档和之前的问题,但都提到使用 setAnimationRepeatCount,但 xcode 有一个错误,指出它在 iOS13 中已贬值(这也不起作用) .播放一次后是否有任何内置函数可以用来停止动画?我在某处读到说使用回调函数并重新初始化动画,但我不确定该怎么做。还是使用 UIViewPropertyAnimator 而不是 UIView.animate 更好?
我刚刚开始学习Swift,非常感谢任何帮助或指导!
您可以使用以下方法来实现您的目标:
UIView.animateKeyframes(withDuration: 2, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
self.ball.alpha = 0.0
}
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
self.ball.alpha = 1
}
}) { (isFinished) in
}
iOS 13 已弃用方法的替代方法是 https://developer.apple.com/documentation/uikit/uiview/3043564-modifyanimations。示例:
UIView.animate(withDuration:1, animations: {
UIView.modifyAnimations(withRepeatCount: 3, autoreverses: true, animations: {
// whatever
})
})
但是,已弃用的方法仍然有效;它只是被弃用了。
而不是最后一个 }) 使用:
}, completion: { finished in
self.alpha = 1
})
Swift 5.x
当前 iOS 已弃用方法的语法替换可以像下面的示例一样编写:
UIView.animate(withDuration:1,
delay: 0.5,
options: [.curveEaseInOut, .autoreverse, .repeat]) {
UIView.modifyAnimations(withRepeatCount: 3, autoreverses: true) {
print("repeated stuff")
}
} completion: { _ in
print("done")
}