在Swift

时间:2016-04-03 18:58:05

标签: ios swift animation

我有一个关键帧动画来显示Swift中的卡片处理效果。如果按一个按钮,我会使用第二个动画(使用.BeginFromCurrentState ...)来取消交易。这对简单的单视图动画有效。然而,按下按钮后,这里有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,但是在运行完成处理程序时可能会出现同样的延迟问题?

2 个答案:

答案 0 :(得分:2)

要取消动画,只需对要动画的每个图层(或每个视图的图层)说removeAllAnimations即可。您还需要确定您希望该视图出现的位置,但这是一个不同的问题(换句话说,您必须考虑取消实际包含的内容)。

答案 1 :(得分:0)

问题的直接答案(本身提出了另一个问题)是,stopAnimation(使用.BeginFromCurrentState调用另一个动画)或..layer.removeAllAnimations实际上会取消动画。但是在我的情况下,在完成块中还有其他的UI工作,显然直到预定的时间才会运行。换句话说,即使您单独取消/删除关键帧动画,也会延迟完成块。