Ios Swift中关键帧动画的有效消除
我有一个关键帧动画来显示Swift中的牌处理效果。如果您按下按钮,我将使用第二个动画(使用.beginforomcurrentstate…)取消交易。这对简单的单视图动画非常有效。但是,按下按钮后会有2-10秒的延迟,可能是在运行每个取消动画时。是否有一种更简单、更顺畅的方式来实现我想要的(立即取消交易) 下面是设置交易动画的代码片段:Ios Swift中关键帧动画的有效消除,ios,swift,animation,Ios,Swift,Animation,我有一个关键帧动画来显示Swift中的牌处理效果。如果您按下按钮,我将使用第二个动画(使用.beginforomcurrentstate…)取消交易。这对简单的单视图动画非常有效。但是,按下按钮后会有2-10秒的延迟,可能是在运行每个取消动画时。是否有一种更简单、更顺畅的方式来实现我想要的(立即取消交易) 下面是设置交易动画的代码片段: let durationSlice = 1.0/Double(numCards*numPlayers+1) var durationSliceS
let durationSlice = 1.0/Double(numCards*numPlayers+1)
var durationSliceStart : Double = 0
UIView.animateKeyframesWithDuration(FiveKings.ANIMATION_250MS*(Double(numCards)*Double(numPlayers)+1), delay: 0.0 ,
options: [.CalculationModeCubic],
animations: {
for iCard in 0..<numCards {
durationSliceStart = Double(iCard*numPlayers) * durationSlice
//translate the cards off the pile and to each mini Hand (and they stay visible)
for iPlayer in 0..<numPlayers {
//Animate the card into view at the start of this set
UIView.addKeyframeWithRelativeStartTime(durationSliceStart+Double(iPlayer)*durationSlice, relativeDuration: 0.0, animations: {
pileCardViews[iCard*numPlayers + iPlayer].alpha = 1.0
})
//add a random amount of translation and rotation to simulate messy cards
let messyX = CGFloat((drand48()-0.5) * MESSY_CARD_XY_OFFSET) * self.mDrawPile.bounds.width
let messyY = CGFloat((drand48()-0.5) * MESSY_CARD_XY_OFFSET) * self.mDrawPile.bounds.height
let messyRotation = CGFloat((drand48()-0.5) * MESSY_CARD_ANGLE) * 2.0 * 3.14
//convert the destination miniHand into the coordinates of mDrawPile
let miniHandLayout = self.mGame.players.getPlayer(iPlayer).miniHandLayout!
miniHandDestinationPoint = miniHandLayout.cardView.convertPoint(CGPoint(x: 0,y: 0), toView: self.mDrawPile)
UIView.addKeyframeWithRelativeStartTime(durationSliceStart+Double(iPlayer)*durationSlice, relativeDuration: durationSlice, animations: {
pileCardViews[iCard*numPlayers + iPlayer].transform = CGAffineTransformConcat(
CGAffineTransformMakeRotation(3.14+messyRotation),
CGAffineTransformMakeTranslation(miniHandDestinationPoint.x + messyX, miniHandDestinationPoint.y + messyY))
if iCard == 0 {miniHandLayout.cardView.alpha = 1.0}
})
}
}//end iCard
//animate a card to the DiscardPile
let discardPileDestination = self.mDiscardPile.convertPoint(CGPoint(x: 0, y: 0), toView: self.mDrawPile)
UIView.addKeyframeWithRelativeStartTime(durationSliceStart+Double(numPlayers)*durationSlice, relativeDuration: durationSlice, animations:
{
pileCardViews[numCards*numPlayers].transform = CGAffineTransformMakeTranslation(discardPileDestination.x, discardPileDestination.y)
pileCardViews[numCards*numPlayers].alpha = 1.0
})
},
//completion block removes the added cards
completion: {(_ : Bool) in
//remove the added cards
for pcv in pileCardViews {pcv.removeFromSuperview()}
self.afterDealing()
}
)//end UIView.animateKeyFramesWithDuration
stopAnimation
的实现是UIView的扩展:
func stopAnimation() {
UIView.animateWithDuration(0.0, delay: 0.0, options: [.BeginFromCurrentState],
animations: {
self.alpha = 1.0
self.transform = CGAffineTransformIdentity
}, completion: nil)
}
编辑:我尝试过使用
…layer.removeAllAnimations
,但同样的延迟问题也会出现,可能是在运行完成处理程序时?要取消动画,只需对正在设置动画的每个层(或每个视图的层)说removeAllAnimations
。您还需要决定现在希望视图显示在何处,但这是一个不同的问题(换句话说,您必须考虑取消实际上包括哪些内容)。要取消动画,只需对正在设置动画的每个层(或每个视图的层)说removeAllAnimations
。您还需要决定现在希望该视图显示在何处,但这是一个不同的问题(换句话说,您必须考虑取消实际上包括哪些内容)。该问题的直接答案(它本身提出了另一个问题)是停止动画
(使用.BeginFromCurrentState
调用另一个动画)或者.layer.removeAllimations
实际上会取消动画。但是在我的例子中,完成块中有额外的UI工作正在进行,显然直到计划的时间才运行。换句话说,即使您单独取消/删除关键帧动画,完成块也会延迟。直接这个问题的答案(它本身提出了另一个问题)是停止动画
(使用.BeginFromCurrentState
调用另一个动画)或者.layer.removeAllAnimations
实际上会取消动画。但是在我的例子中,完成块中有额外的UI工作正在进行,显然直到计划的时间才运行。换句话说,即使您单独取消/删除关键帧动画,完成块也会延迟。是的,我尝试过这已经发生了。同样的问题也发生了-removeAllAnimations完成时有一个长时间的暂停。“removeAllAnimations完成时有一个长时间的暂停”真的吗?你确定没有线程问题吗?在我上面发布的代码片段中,如果我用pcv.layer.removeAllAnimations()替换pcv.stopAnimation(),我会得到同样的“暂停”在UI中。你能告诉我我能做些什么来追踪“线程问题”吗?没有其他事情发生。谢谢@matt的怀疑,因为它为我指明了正确的方向。请看我的答案。是的,我已经试过了。同样的问题也发生了-在RemoveAllanimation完成时有一个长时间的停顿。“removeAllAnimations完成时会有一个长时间的暂停”真的吗?你确定没有线程问题吗?在我上面发布的代码片段中,如果我用pcv.layer.removeAllAnimations()替换pcv.stopAnimation(),我在UI中也会遇到同样的“暂停”。你能告诉我如何跟踪线程问题吗“?没有其他事情发生。谢谢@matt的怀疑,因为它为我指明了正确的方向。看看我的答案。”。
func stopAnimation() {
UIView.animateWithDuration(0.0, delay: 0.0, options: [.BeginFromCurrentState],
animations: {
self.alpha = 1.0
self.transform = CGAffineTransformIdentity
}, completion: nil)
}