1

我有一个动画,我使用推送动画,然后使用快照动画UIDynamicBehavior,然后我以属性行为结束:

for card in selectedCards {
        removeCard(card: card)
      }

  private func removeCard(card: Card) {
    guard let subView = cardsContainer.subviews.first(where: { ($0 as? PlayingCardView)?.card == card }) else {
      return
    }
    if let card = subView as? PlayingCardView { card.selected = false }
    let matchedCardsFrame = matchedCards.convert(matchedCards.frame, to: view)
    view.addSubview(subView)
    cardBehavior.addItem(subView) // here I add the push behavior
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
      self.cardBehavior.removeItem(subView) // here I remove the push behavior
      UIViewPropertyAnimator.runningPropertyAnimator(
        withDuration: 0.3,
        delay: 0,
        options: [],
        animations: {
          self.cardBehavior.addSnapBehavior(subView, frame: matchedCardsFrame) // here I add the snap behavior
      }, completion: { finished in
          self.animator.removeAllBehaviors()
          subView.frame.size = CGSize(width: matchedCardsFrame.height, height: matchedCardsFrame.width)
          subView.transform = CGAffineTransform.identity.rotated(by: CGFloat.pi / 2)
          subView.setNeedsDisplay()
      })
    }
  }

基本上上面的代码做了以下事情:

  • 添加推送行为
  • 移除推送行为
  • 添加捕捉行为
  • 删除所有行为
  • 添加属性转换

我想要的是执行推送操作,然后在大约一秒钟后执行快照行为,并在快照执行完成后执行转换。但是,如果我removeAllBehaviors()在执行属性转换之前,则快照行为不会完成。但是,如果我离开快照行为并尝试执行属性转换,则它没有任何效果,因为快照行为似乎无限期地作用于对象,使其与属性转换不一致。

我如何以编程方式说完成快照行为然后执行转换?

4

0 回答 0