我有一个动画,其中使用推式动画,然后使用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()
})
}
}
基本上,以上代码执行以下操作:
我想要的是执行push操作,然后在大约一秒钟后执行snap行为,并在完成snap执行之后执行转换。但是,如果我在执行属性转换之前removeAllBehaviors()
,则快照行为不会结束。但是,如果我离开捕捉行为并尝试执行属性转换,则它没有任何作用,因为看起来捕捉行为会无限期地作用于对象,这与属性转换不符。
我如何以编程方式说完成捕捉行为,然后执行转换?