在Swift中做一个垂直翻转动画



我目前正试图在我的项目的子视图中获得我的卡的垂直翻转动画。我使用了一些依赖关系(Shuffle Cocoapod),我尝试了所有方法来获得从右到左的翻转动画。似乎我可以改变每张卡片的背景和其他简单的东西,但当我试图动画它时什么也没有发生。目标是"露出卡片的背面"。里面有另一个词(基本上每张卡片都有一个"前词")。还有一个"背词"。我说错了什么?

import UIKit
import Shuffle

class CardsViewController: UIViewController, SwipeCardStackDataSource, SwipeCardStackDelegate {

let cardStack = SwipeCardStack()
var actualIndex : Int = 0
var showingBack = false

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(cardStack)
cardStack.frame.size.height = 512
cardStack.frame.size.width = 384
cardStack.center.y =  CGFloat(cardStack.frame.size.height/2)
cardStack.center.x =  CGFloat(cardStack.frame.size.width/2)
cardStack.center = view.center
cardStack.dataSource = self
}


func card(fromImage word: String) -> SwipeCard {
let card = SwipeCard()
card.swipeDirections = [.left, .right]
for direction in card.swipeDirections {
card.setOverlay(CardOverlay(direction: direction), forDirection: direction)
}
card.content = CardContentView(withWord: word)
return card
}


func cardStack(_ cardStack: SwipeCardStack, cardForIndexAt index: Int) -> SwipeCard {
let  theCard = card(fromImage: cardWords[actualIndex].word)
actualIndex = actualIndex + 1
return theCard
}
func numberOfCards(in cardStack: SwipeCardStack) -> Int {
return cardWords.count
}

let cardWords = [
DataOfWord(word: "comme", translation: "as"),
DataOfWord(word: "je", translation: "I"),
DataOfWord(word: "son", translation: "his"),
DataOfWord(word: "que", translation: "that")]
}

你可以用UIView的transition(with:duration:options:animations:completion:)方法创建一个翻转类型的动画

这是我自己的一个应用程序中的一些代码,我翻转游戏板以在"另一边"显示关卡完成分数:

UIView.transition(with: gameBoardCollectionView, duration: 0.75, options: [.transitionFlipFromRight]) {
self.loadLevelEndArcadeView()
} completion: { (complete) in
self.animateLevelEndArcadeLabelViews()
}

在过渡代码块中,你只是加载你想要的视图当动画完成时,你会显示一些代表视图背面的东西。如果你想的话,你也可以调用其他的过渡动画。

最新更新