如何获得动画视图的正确位置?

时间:2018-06-04 10:31:06

标签: ios swift

我想创建一套游戏App。为此,我有一副卡片,我希望卡片(动画)在棋盘上移动。

我的问题是当我这样做时:

   private func createDeck() {
        allCards.forEach{ card in
            card.isFaceUp = false
            card.frame = Deck.frame
            GameBoard.allCardsInDeck.append(card)
        }
    }
卡片从另一个位置出现,然后是卡片。

Deck.frame superview不能给我deckView视图的当前位置吗?我的卡片从顶部显示,但我的import UIKit class ViewController: UIViewController { @IBOutlet weak var informationLabel: UILabel! @IBOutlet weak var ScoreLabel: UILabel! @IBOutlet weak var Deck: Deck! @IBOutlet weak var GameBoard: CardBoardView! { didSet { let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipe)) swipeGesture.direction = .down GameBoard.addGestureRecognizer(swipeGesture) let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotation)) GameBoard.addGestureRecognizer(rotationGesture) } } private var gameIsStarted = false private var game: Game = Game() { didSet { updateScoreLabel() } } private var selectedCards = [CardSubview] () private var indexOfAllCards: Int = 0 private lazy var allCards = getAllCards() private var cardsBackgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) @objc func rotation(recognizer: UIRotationGestureRecognizer){ switch recognizer.state { case .ended: GameBoard.mixCards() default: break } } @objc private func swipe(recognizer: UISwipeGestureRecognizer) { switch recognizer.state { case .ended: add3Cards() default: break } } private func getAllCards () -> [CardSubview] { var cards = [CardSubview]() for index in 0..<game.cards.count { cards.append(CardSubview()) cards[index].fill = game.cards[index].strokeIdentifier cards[index].color = game.cards[index].colorIdentifier cards[index].form = game.cards[index].form cards[index].occurence = game.cards[index].occurenceOfForm cards[index].addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tab))) cards[index].backgroundColor = cardsBackgroundColor cards[index].indexNr = index } return cards } @objc private func tab (recognizer: UITapGestureRecognizer) { switch recognizer.state { case .ended: if let card = recognizer.view as? CardSubview, card.isFaceUp == true { card.isSelected = !card.isSelected if card.isSelected { selectedCards.append(card) if selectedCards.count > 2 { let isSet = game.isSet(arrayOfIndexes: [selectedCards[0].indexNr,selectedCards[1].indexNr, selectedCards[2].indexNr]) == true if isSet { game.score += 1 selectedCards.forEach { card in GameBoard.doWhenMatched(card: card) card.isSelected = false selectedCards.removeAll() } findSet() } else if isSet == false { game.score -= 1 selectedCards.forEach{ card in card.isSelected = false } selectedCards.removeAll() } } } else if card.isSelected == false, selectedCards.contains(card) { selectedCards.remove(at: selectedCards.index(of: card)!) } } default: break } } private func findSet() { if GameBoard.allOpenCards.count > 0 { var setInOpenCards = Bool() var allSetsInOpenCards = Int() for index1 in 0..<GameBoard.allOpenCards.count-2 { for index2 in (index1+1)..<GameBoard.allOpenCards.count-1 { for index3 in (index2+1)..<GameBoard.allOpenCards.count { setInOpenCards = game.isSet(arrayOfIndexes: [allCards[index1].indexNr, allCards[index2].indexNr, allCards[index3].indexNr]) if (setInOpenCards == true){ allSetsInOpenCards+=1 } } } } informationLabel.text = "Sets available: \(allSetsInOpenCards)" } else { informationLabel.text = "Sets available: \(0)" } } @IBAction private func newGame() { game = Game() GameBoard.removeSubviewsFromDeck() GameBoard.allCardsInDeck.removeAll() createDeck() GameBoard.backgroundColor = UIColor.black GameBoard.removeSubviews() selectedCards.removeAll() gameIsStarted = true allCards = getAllCards() indexOfAllCards = 0 GameBoard.allOpenCards.removeAll() add12Cards() } private func updateScoreLabel () { if game.score >= 0 { ScoreLabel.backgroundColor = #colorLiteral(red: 0, green: 0.9768045545, blue: 0, alpha: 0.5) } else if game.score < 0 { ScoreLabel.backgroundColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 0.5) } ScoreLabel.text = "Score: \(game.score)" } private func add3Cards() { if indexOfAllCards < allCards.count, gameIsStarted == true { for _ in 0...2 { GameBoard.allOpenCards.append(GameBoard.allCardsInDeck[indexOfAllCards]) indexOfAllCards+=1 } } findSet() } private func add12Cards() { let numbersOfFirst12Cards = 12 if gameIsStarted == true { for _ in 0..<numbersOfFirst12Cards { GameBoard.allOpenCards.append(GameBoard.allCardsInDeck[indexOfAllCards]) indexOfAllCards += 1 } } findSet() } private func createDeck() { allCards.forEach{ card in card.isFaceUp = false card.frame = Deck.frame GameBoard.allCardsInDeck.append(card) } } } 位于底部。

这是我的代码ViewController:

CardBoardView

这是我import UIKit @IBDesignable class CardBoardView: UIView { let durationForDisappearingCards = 2.0 let delayForDisappearingCards = 0.0 lazy var animator = UIDynamicAnimator(referenceView: self) lazy var cardBehavior = CardBehavior(in: animator) var allOpenCards = [CardSubview]() { didSet { addSubview(); setNeedsLayout();} } var allCardsInDeck = [CardSubview] () { didSet{ setNeedsLayout()} } struct Layout { static let ratio:CGFloat = 2.0 static let insetByX:CGFloat = 2.0 static let insetByY:CGFloat = 2.0 } private func addSubview() { for card in allOpenCards { addSubview(card) } } public func removeSubviews() { for card in allOpenCards { card.removeFromSuperview() } } public func removeSubviewsFromDeck() { allCardsInDeck.forEach{ card in card.removeFromSuperview() } } public func mixCards() { var mixCards = [CardSubview]() for _ in 0..<allOpenCards.count { let random = allOpenCards.count.arc4random let card = allOpenCards[random] mixCards.append(card) allOpenCards.remove(at: random) } allOpenCards = mixCards } public func doWhenMatched(card: CardSubview) { // cardBehavior.addItem(card) UIView.animate( withDuration: self.durationForDisappearingCards, delay: self.delayForDisappearingCards, animations: { card.frame = CGRect(x: (self.superview?.frame.midX)!-card.bounds.width/2, y: (self.superview?.frame.minY)!-card.bounds.height, width: card.bounds.width , height: card.bounds.height) }, completion: { finish in card.removeFromSuperview() self.allOpenCards.remove(at: self.allOpenCards.index(of: card)!) } ) } // let newPosition = CGRect(x: self.frame.midX-card.bounds.width/2 , y: self.frame.minY, width: card.bounds.width, height: card.bounds.height) // // // // UIView.animate(withDuration: durationForDisappearingCards, delay: delayForDisappearingCards, options: [.allowUserInteraction], animations: {card.alpha=0.0}) override func layoutSubviews() { super.layoutSubviews() var grid = Grid(layout: .aspectRatio(Layout.ratio), frame: self.bounds) grid.cellCount = allOpenCards.count var secondsForDelay:CGFloat = 0 let secondsForFlipCard:CGFloat = 0.5 for index in allOpenCards.indices { if self.allOpenCards[index].frame != (grid[index]?.insetBy(dx: Layout.insetByX, dy: Layout.insetByY)) ?? CGRect.zero { UIView.animate( withDuration: 0.5, delay: TimeInterval(secondsForDelay) , animations: {self.allOpenCards[index].frame = (grid[index]?.insetBy(dx: Layout.insetByX, dy: Layout.insetByY)) ?? CGRect.zero}, completion: { finish in if self.allOpenCards[index].isFaceUp != true { UIView.transition(with: self.allOpenCards[index], duration: TimeInterval(secondsForFlipCard), options: [.transitionFlipFromLeft], animations: { self.allOpenCards[index].isFaceUp = true } ) } } ) secondsForDelay+=0.02 } } } } private extension Int { var arc4random: Int { if self > 0 { return Int(arc4random_uniform(UInt32(self))) } else if self < 0 { return -Int(arc4random_uniform(UInt32(self))) } else { return 0 } } } private extension CGFloat { var arc4random: CGFloat { if self > 0 { return CGFloat(arc4random_uniform(UInt32(self))) } else if self < 0 { return -CGFloat(arc4random_uniform(UInt32(self))) } else { return 0 } } } 所有动画发生的地方

var a = 1; 
var b = 2; 
var c = (a,b);
console.log(c);
//output : as expected 
var c = a,b;
console.log(c);
//output : 1 

0 个答案:

没有答案