我有一个关键帧动画显示一个快速的卡交易效果。我使用第二个动画(使用.beginfraomcurrentstate…)取消交易,如果你按下按钮。这对简单的单视图动画有效。但是在这里,按下按钮后会有2-10秒的延迟,大概是在运行每个取消动画时。有没有一种更简单、更顺畅的方法来实现我想要的目标(立即取消交易)。
下面是设置交易动画的代码片段:

    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

以下是按“跳过交易”按钮时运行的代码:
    if !mDealingPileCards.isEmpty {
        for pcv in self.mDealingPileCards {pcv.stopAnimation()} //also seems to call completion handler
        self.mDealingPileCards.removeAll(keepCapacity: true)
        self.setShowHint(stringKey: "toDisableDealing", setShowHint: FiveKings.HandleHint.SET_AND_SHOW_HINT, hintLevel: GameDifficulty.MEDIUM)
    }

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。您还需要决定现在希望该视图出现在什么地方,但这是一个不同的问题(换句话说,您必须考虑取消实际上包括什么)。

10-06 16:11
查看更多