Ios 动画未停止使用UIView.animate
我的代码使用的是一个简单的循环动画,在5秒的持续时间后不会停止。我想做的是有第二个按钮,停止动画。正如您在我的代码中所看到的,Ios 动画未停止使用UIView.animate,ios,swift,uiimageview,uiviewanimation,completion,Ios,Swift,Uiimageview,Uiviewanimation,Completion,我的代码使用的是一个简单的循环动画,在5秒的持续时间后不会停止。我想做的是有第二个按钮,停止动画。正如您在我的代码中所看到的,circle.stopaniting()没有任何效果 class VET: UIViewController { @IBOutlet weak var circle: UIImageView! var duration = 5 @IBAction func start(_ sender: Any) { UIView.animate
circle.stopaniting()
没有任何效果
class VET: UIViewController {
@IBOutlet weak var circle: UIImageView!
var duration = 5
@IBAction func start(_ sender: Any) {
UIView.animate(withDuration: TimeInterval(duration), delay: 0.5, options: [.repeat, .autoreverse, .curveEaseIn, .curveEaseOut], animations: { () -> Void in
let scale = CGAffineTransform(scaleX: 0.25, y: 0.25)
self.circle.transform = scale
print("animation")
}, completion: { _ in
//if finished { or (finished: Bool)
// if isAnimating {
if self.circle.isAnimating {
self.circle.stopAnimating()
print("is animating -- stop animating")
} else {
self.circle.startAnimating()
print("start animating")
}
})
}
@IBAction func stop() {
circle.stopAnimating()
}
开始设置和停止设置动画,以便在某些时间间隔内更改imageview的图像,如GIF。在这里,您将动画块与“重复”一起使用,并且仅当动画被中断时才会调用“完成”块。例如,当应用程序进入后台并再次返回前台时,它会被调用 要在某个间隔后停止动画,必须在该间隔后强制调用removeAllAnimations。请参阅以下代码:
@IBOutlet weak var circle: UIImageView!
var duration = 10
func start() {
UIView.animate(withDuration: TimeInterval(duration), delay: 0.5, options: [.repeat, .autoreverse, .curveEaseIn, .curveEaseOut], animations: { () -> Void in
let scale = CGAffineTransform(scaleX: 0.25, y: 0.25)
self.circle.transform = scale
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(self.duration * 1000)) {
// Code
self.circle.layer.removeAllAnimations()
}
print("animation")
}, completion: { _ in
let scale = CGAffineTransform(scaleX: 1.0, y: 1.0)
self.circle.transform = scale
})
}
开始设置和停止设置动画,以便在某些时间间隔内更改imageview的图像,如GIF。在这里,您将动画块与“重复”一起使用,并且仅当动画被中断时才会调用“完成”块。例如,当应用程序进入后台并再次返回前台时,它会被调用 要在某个间隔后停止动画,必须在该间隔后强制调用removeAllAnimations。请参阅以下代码:
@IBOutlet weak var circle: UIImageView!
var duration = 10
func start() {
UIView.animate(withDuration: TimeInterval(duration), delay: 0.5, options: [.repeat, .autoreverse, .curveEaseIn, .curveEaseOut], animations: { () -> Void in
let scale = CGAffineTransform(scaleX: 0.25, y: 0.25)
self.circle.transform = scale
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(self.duration * 1000)) {
// Code
self.circle.layer.removeAllAnimations()
}
print("animation")
}, completion: { _ in
let scale = CGAffineTransform(scaleX: 1.0, y: 1.0)
self.circle.transform = scale
})
}
如果需要对动画进行更多控制,则使用错误的方法来使用动画 而是使用UIViewPropertyAnimator 替换属性animator初始值设定项返回需要启动的animator对象:
let animator = UIViewPropertyAnimator(duration: duration, curve: .linear) {
self.redSquare.backgroundColor = .green
}
animator.startAnimation()
类似地,可以使用停止与动画师相关的动画
animator.stopAnimation()
如果需要对动画进行更多控制,则使用错误的方法来使用动画 而是使用UIViewPropertyAnimator 替换属性animator初始值设定项返回需要启动的animator对象:
let animator = UIViewPropertyAnimator(duration: duration, curve: .linear) {
self.redSquare.backgroundColor = .green
}
animator.startAnimation()
类似地,可以使用停止与动画师相关的动画
animator.stopAnimation()
1. <代码>UIImage开始/停止动画制作()与
UIView.animate
无关。如果设置图像视图的animationImages
属性,则它们将使用。2.UIView.animate
completion块在动画完成之前(在本例中为5.5秒后)不会调用。即使在5.5秒后,也不会发生任何事情。此动画以无休止的循环进行。请再次阅读我的第一点。了解如何设置image views属性。如果我只想将动画设置为运行10秒并停止,我会在属性中写入什么。1<代码>UIImage开始/停止动画制作()与UIView.animate
无关。如果设置图像视图的animationImages
属性,则它们将使用。2.UIView.animate
completion块在动画完成之前(在本例中为5.5秒后)不会调用。即使在5.5秒后,也不会发生任何事情。此动画以无休止的循环进行。请再次阅读我的第一点。了解如何设置image views属性。如果我只想将动画设置为运行10秒并停止,我会在属性中写入什么。