首页 > 解决方案 > 删除和替换子视图时如何处理它们?

问题描述

我想为iOS制作游戏“SET”。

现在我想做的第一件事是将前 12 张卡片放在屏幕上。

处理出牌的位置老师给了我们这个格子。

//
//  Grid.swift
//
//  Created by CS193p Instructor.
//  Copyright © 2017 Stanford University. All rights reserved.
//
//  Arranges the space in a rectangle into a grid of cells.
//  All cells will be exactly the same size.
//  If the grid does not fill the provided frame edge-to-edge
//    then it will center the grid of cells in the provided frame.
//  If you want spacing between cells in the grid, simply inset each cell's frame.
//
//  How it lays the cells out is determined by the layout property:
//  Layout can be done by (a) fixing the cell size
//    (Grid will create as many rows and columns as will fit)
//  Or (b) fixing the number of rows and columns
//    (Grid will make the cells as large as possible)
//  Or (c) ensuring a certain aspect ratio (width vs. height) for each cell
//    (Grid will make cellCount cells fit, making the cells as large as possible)
//    (you must set the cellCount var for the aspectRatio layout to know what to do)
//
//  The bounding rectangle of a cell in the grid is obtained by subscript (e.g. grid[11] or grid[1,5]).
//  The dimensions tuple will contain the number of (calculated or specified) rows and columns.
//  Setting aspectRatio, dimensions or cellSize, may change the layout.
//
//  To use, simply employ the initializer to choose a layout strategy and set the frame.
//  After creating a Grid, you can change the frame or layout strategy at any time
//    (all other properties will immediately update).

import UIKit

struct Grid
{
    enum Layout {
        case fixedCellSize(CGSize)
        case dimensions(rowCount: Int, columnCount: Int)
        case aspectRatio(CGFloat)
    }

    var layout: Layout { didSet { recalculate() } }

    var frame: CGRect { didSet { recalculate() } }

    init(layout: Layout, frame: CGRect = CGRect.zero) {
        self.frame = frame
        self.layout = layout
        recalculate()
    }

    subscript(row: Int, column: Int) -> CGRect? {
        return self[row * dimensions.columnCount + column]
    }

    subscript(index: Int) -> CGRect? {
        return index < cellFrames.count ? cellFrames[index] : nil
    }

    var cellCount: Int {
        get {
            switch layout {
            case .aspectRatio: return cellCountForAspectRatioLayout
            case .fixedCellSize, .dimensions: return calculatedDimensions.rowCount * calculatedDimensions.columnCount
            }
        }
        set { cellCountForAspectRatioLayout = newValue }
    }

    var cellSize: CGSize {
        get { return cellFrames.first?.size ?? CGSize.zero }
        set { layout = .fixedCellSize(newValue) }
    }

    var dimensions: (rowCount: Int, columnCount: Int) {
        get { return calculatedDimensions }
        set { layout = .dimensions(rowCount: newValue.rowCount, columnCount: newValue.columnCount) }
    }

    var aspectRatio: CGFloat {
        get {
            switch layout {
            case .aspectRatio(let aspectRatio):
                return aspectRatio
            case .fixedCellSize(let size):
                return size.height > 0 ? size.width / size.height : 0.0
            case .dimensions(let rowCount, let columnCount):
                if rowCount > 0 && columnCount > 0 && frame.size.width > 0 {
                    return (frame.size.width / CGFloat(columnCount)) / (frame.size.width / CGFloat(rowCount))
                } else {
                    return 0.0
                }
            }
        }
        set { layout = .aspectRatio(newValue) }
    }

    private var cellFrames = [CGRect]()
    private var cellCountForAspectRatioLayout = 0 { didSet { recalculate() } }
    private var calculatedDimensions: (rowCount: Int, columnCount: Int) = (0, 0)

    private mutating func recalculate() {
        switch layout {
        case .fixedCellSize(let cellSize):
            if cellSize.width > 0 && cellSize.height > 0 {
                calculatedDimensions.rowCount = Int(frame.size.height / cellSize.height)
                calculatedDimensions.columnCount = Int(frame.size.width / cellSize.width)
                updateCellFrames(to: cellSize)
            } else {
                assert(false, "Grid: for fixedCellSize layout, cellSize height and width must be positive numbers")
                calculatedDimensions = (0, 0)
                cellFrames.removeAll()
            }
        case .dimensions(let rowCount, let columnCount):
            if columnCount > 0 && rowCount > 0 {
                calculatedDimensions.rowCount = rowCount
                calculatedDimensions.columnCount = columnCount
                let cellSize = CGSize(width: frame.size.width/CGFloat(columnCount), height: frame.size.height/CGFloat(rowCount))
                updateCellFrames(to: cellSize)
            } else {
                assert(false, "Grid: for dimensions layout, rowCount and columnCount must be positive numbers")
                calculatedDimensions = (0, 0)
                cellFrames.removeAll()
            }
        case .aspectRatio:
            assert(aspectRatio > 0, "Grid: for aspectRatio layout, aspectRatio must be a positive number")
            let cellSize = largestCellSizeThatFitsAspectRatio()
            if cellSize.area > 0 {
                calculatedDimensions.columnCount = Int(frame.size.width / cellSize.width)
                calculatedDimensions.rowCount = (cellCount + calculatedDimensions.columnCount - 1) / calculatedDimensions.columnCount
            } else {
                calculatedDimensions = (0, 0)
            }
            updateCellFrames(to: cellSize)
            break
        }
    }

    private mutating func updateCellFrames(to cellSize: CGSize) {
        cellFrames.removeAll()

        let boundingSize = CGSize(
            width: CGFloat(dimensions.columnCount) * cellSize.width,
            height: CGFloat(dimensions.rowCount) * cellSize.height
        )
        let offset = (
            dx: (frame.size.width - boundingSize.width) / 2,
            dy: (frame.size.height - boundingSize.height) / 2
        )
        var origin = frame.origin
        origin.x += offset.dx
        origin.y += offset.dy

        if cellCount > 0 {
            for _ in 0..<cellCount {
               cellFrames.append(CGRect(origin: origin, size: cellSize))
                origin.x += cellSize.width
                if round(origin.x) > round(frame.maxX - cellSize.width) {
                    origin.x = frame.origin.x + offset.dx
                    origin.y += cellSize.height
                }
            }
        }
    }

    private func largestCellSizeThatFitsAspectRatio() -> CGSize {
        var largestSoFar = CGSize.zero
        if cellCount > 0 && aspectRatio > 0 {
            for rowCount in 1...cellCount {
                largestSoFar = cellSizeAssuming(rowCount: rowCount, minimumAllowedSize: largestSoFar)
            }
            for columnCount in 1...cellCount {
                largestSoFar = cellSizeAssuming(columnCount: columnCount, minimumAllowedSize: largestSoFar)
            }
        }
        return largestSoFar
    }

    private func cellSizeAssuming(rowCount: Int? = nil, columnCount: Int? = nil, minimumAllowedSize: CGSize = CGSize.zero) -> CGSize {
        var size = CGSize.zero
        if let columnCount = columnCount {
            size.width = frame.size.width / CGFloat(columnCount)
            size.height = size.width / aspectRatio
        } else if let rowCount = rowCount {
            size.height = frame.size.height / CGFloat(rowCount)
            size.width = size.height * aspectRatio
        }
        if size.area > minimumAllowedSize.area {
            if Int(frame.size.height / size.height) * Int(frame.size.width / size.width) >= cellCount {
                return size
            }
        }
        return minimumAllowedSize
    }
}

private extension CGSize {
    var area: CGFloat {
        return width * height
    }
}

现在我在我的 ClassBoardView 中使用了该网格,如下所示:

import UIKit
@IBDesignable

class CardBoardView: UIView {
    public lazy var grid = Grid(layout: .aspectRatio(values.ratio), frame: self.bounds)
    var cards = [CardSubview]()
    public var cells = 12
    private var selectedCards = [Int]()
    private var deck = gameDeck(numbersOfCards: 81)
    private var indexOfCards = 0
    let values = Values()
    var index1 = 1
    var index2 = 1
    var index3 = 1
    var index4 = 1

    struct Values {
        public let ratio:CGFloat = 2.0
        public let insetByX:CGFloat = 2.0
        public let insetByY:CGFloat = 2.0
        public let allAvailableCards = 81
    }

    @objc private func tab (recognizer: UITapGestureRecognizer) {
        switch recognizer.state {
        case .ended:
            if let card = recognizer.view as? CardSubview {
                card.isSelected = !card.isSelected
                if card.isSelected == true {
                    selectedCards.append(cards.index(of: card)!)
                    if selectedCards.count == 3 {
                        if isSet(indexes: selectedCards) == true {
                            selectedCards.forEach { index in
                                cards[index].removeFromSuperview()
                                cards.remove(at: index)
                            }
                        } else {
                            selectedCards.forEach{ index in
                                cards[index].isSelected = false
                            }
                            selectedCards.removeAll()
                        }
                    }
                } else if card.isSelected == false {
                    if selectedCards.contains(cards.index(of: card)!) {
                        selectedCards.remove(at: selectedCards.index(of: cards.index(of:card)!)!)
                    }
                }
            }
        default: break
        }
    }

    private func isSet(indexes: [Int]) -> Bool {
        return deck.isSet(arrayOfIndexes: indexes)
    }

    private func nextIndex() {
        index1+=1
        if index1==4 {
            index1=1
            index2+=1
            if index2==4 {
                index2=1
                index3+=1
                if index3==4 {
                    index3=1
                    index4+=1
                    if index4==4 {
                        index4=1
                    }
                }
            }
        }
    }



    override func layoutSubviews() {
        super.layoutSubviews()
        grid.cellCount = cells

        for index in 0...80 {
            cards.append(CardSubview())
            cards[index].index1=index1
            cards[index].index2=index2
            cards[index].index3=index3
            cards[index].index4=index4
            nextIndex()
        }

        for index in 0..<grid.cellCount{
            let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tab))
            cards[indexOfCards].addGestureRecognizer(tapRecognizer)
            cards[indexOfCards].frame = grid[index]!.insetBy(dx: values.insetByX, dy: values.insetByY)
            addSubview(cards[indexOfCards])
            indexOfCards+=1
        }
    }
}

现在我的问题是,当我选择 3 张卡片时,应该检查它们是否有一组。如果有 Set,则应移除所有选定的卡片 (3)。此时应该不会发生任何其他事情,但是当运行我的代码并选择 3 张匹配的卡片时,所有卡片都会被接下来的 12 张卡片替换。

有人看到问题了吗?甚至更多的错误?

标签: iosswift

解决方案


您的代码中的一个大问题是您在layoutSubviews(). 你需要以不同的方式构造你的代码,因为layoutSubviews()每当 iOS 想要布局你的视图时都会调用它,这通常发生在某些事情发生变化时。即使一个视图只显示一次,它的 layoutSubviews() 通常也会被调用多次,当然当您向它添加或删除视图时也是如此。

构建代码的一个好方法是将视图创建和其他东西(如附加手势识别器)放置在awakeFromNib()(如果视图是从 nib 文件或情节提要创建的)或init(frame:)(或者如果从代码创建视图,则可能是另一个初始化程序),然后在加载视图时只调用一次。

接下来,在 中,放置layoutSubviews()创建所有对象。请记住,即使在您的视图生命周期中的几乎任何时间和阶段都会调用它,您放在这里的代码也必须有效。

阅读更多 - awakeFromNib()

阅读更多 - init(frame:)

阅读更多 - layoutSubviews()


推荐阅读