基本上,我有一个UITableView
,我想在点击按钮时设置动画。每个单元格应按1.10缩放并返回其变换标识。因此,我使用UIView.animateKeyframesWithDuration
为每个单元格创建了两个不同的动画。
但我想让他们彼此稍微延迟。所以我在这样的循环中调用这个方法:
for (index, cell) in tableView.visibleCells.enumerate() {
let delay = 0.1 * Double(index)
let duration = 0.5
UIView.animateKeyframesWithDuration(duration, delay: delay, options: [], animations: {
UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: duration * 1/2, animations: {
cell.transform = CGAffineTransformScale(cell.transform, 1.10, 1.10)
})
UIView.addKeyframeWithRelativeStartTime(1/2, relativeDuration: duration * 1/2, animations: {
cell.transform = CGAffineTransformIdentity
})
}, completion: nil)
}
问题是完成部分仅适用于每个动画。
我想要的是在每个动画完成后做某事的方法。我怎样才能做到这一点?
答案 0 :(得分:3)
您可以将所有内容都包含在UIView.animateKeyframesWithDuration
调用中并调用单个完成块,如下所示:
let delay = 0.1
let duration = 0.5
let totalDuration = duration + NSTimeInterval(tableView.visibleCells.count - 1) * delay
UIView.animateKeyframesWithDuration(
totalDuration/2,
delay: 0,
options: [],
animations: {
for (index, cell) in self.tableView.visibleCells.enumerate() {
let relativeDelay = delay * Double(index)
print("Cell \(index) animation starts after relative delay: \(relativeDelay)")
UIView.addKeyframeWithRelativeStartTime(relativeDelay, relativeDuration: duration/2, animations: {
cell.transform = CGAffineTransformScale(cell.transform, 1.10, 1.10)
})
UIView.addKeyframeWithRelativeStartTime(relativeDelay + duration/2, relativeDuration: duration/2, animations: {
cell.transform = CGAffineTransformIdentity
})
}
},
completion: { finished in
if finished {
print("Done")
}
})
我希望这就是你想要的。