动画无法正常工作(Swift 3)
问题描述:
以下块是我的代码。我不知道为什么,但动画延迟根本不起作用。无论多少秒我增加它..动画无法正常工作(Swift 3)
func changeLabelisTapped(_ textLabel: UILabel, tapGesture: UITapGestureRecognizer) {
for i in 0...2{
UIView.animate(withDuration: 2, delay: Double(i)+2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({
self.subArray.remove(at: 0)
self.collectionView.deleteItems(at: [IndexPath(row: 0, section: 0)])
}), completion: nil)
}
let tempArray = Array(self.tabledData[3...self.tabledData.count-1])
print(tempArray.count)
for j in 0...2{
UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({
let newElement = tempArray[j]
self.subArray.append(newElement)
self.collectionView.insertItems(at:[IndexPath(row: j, section: 0)])
}), completion: nil)
}
}
答
UIView动画不排队。所以每次你通过一个循环来处理它时,下一个动画会取消之前的动画,而你看不到延迟。不要使用循环,而应该链接动画,以便在完成处理程序中调用下一个动画。删除循环并调用完成处理程序中发生的下一个动画。
var currentCount = 0
UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({
let newElement = tempArray[self.currentCount]
self.subArray.append(newElement)
self.collectionView.insertItems(at:[IndexPath(row: self.currentCount, section: 0)])
}), completion: { _ in
//next animation follows here
self.currentCount += 1
UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({
let newElement = tempArray[self.currentCount]
self.subArray.append(newElement)
self.collectionView.insertItems(at:[IndexPath(row: self.currentCount, section: 0)])
}), completion: { _ in //and so on
})
})
如果您想干净地控制动画发生的次数,那么您可以在完成处理程序中使用递归回调。要做到这一点,请将您的动画代码移到一个函数中。
let loopCount = 3
var currentCount = 0
func animateMyLabel() {
UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({
let newElement = tempArray[self.currentCount]
self.subArray.append(newElement)
self.collectionView.insertItems(at:[IndexPath(row: self.currentCount, section: 0)])
}), completion: { _ in
if self.currentCount < self.loopCount {
self.currentCount += 1
//call the animation again
self.animateMyLabel()
}
})
}
编辑
已经面临目前的一个项目,我做了类似的延迟问题,我学到了新的方式来强制延迟是否如预期般可能帮助任何人是不工作的是遇到这个问题。解决方案是通过添加延迟(秒:n){}来强制在完成处理程序中延迟下一个动画,其中n是延迟应发生的秒数。所以,把它添加到这个问题:
let loopCount = 3
var currentCount = 0
func animateMyLabel() {
UIView.animate(withDuration: 2, delay: 2, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: ({
let newElement = tempArray[self.currentCount]
self.subArray.append(newElement)
self.collectionView.insertItems(at:[IndexPath(row: self.currentCount, section: 0)])
}), completion: { _ in
if self.currentCount < self.loopCount {
//add the delay here
delay(seconds: 2) {
self.currentCount += 1
//call the animation again
self.animateMyLabel()
}
}
})
}
此外,虽然动画不排队,可以使用一个循环的动画提供序列中的下一个动画有一个延迟,它允许一个动画来完成。
它仍然无法正常工作..我将延迟增加到了10秒..动画需要同一时间完成。但谢谢你的回答! – user3608914
您删除了for循环,但它仍然不起作用? – gwinyai
是的,我尝试了你的第二块..它不工作...... :(我看了一个动画教程...视频显示,你不需要完成框来增加延迟..你只是必须增加delayCounter和单元格会相应的布局。所以,我不知道它是否只是插入和删除单元格的问题 – user3608914