FDChessboardView
特点
- 高分辨率图形
- 可定制的主题和游戏图形
- 支持所有单棋盘象棋变体:自杀、败者、原子等。
- 支持奇数棋子排布和非标准将军(Fisher 960)的游戏。
- 非常干净的API,这是一个仅视图
- 支持iOS 8或OS X Mavericks(10.9)及更高版本的最低部署目标。
用法
导入,将视图添加到你的Storyboard,然后使用以下内容设置它:
import FDChessboardView
...
self.chessboard.dataSource = self
然后实现数据源
func chessboardView(board: FDChessboardView, pieceForSquare square: FDChessboardSquare) -> FDChessboardPiece? {
return piecesByIndex[square.index] // you figure out which piece to show
}
这是完整的API
/// The location of a square on a chess board
public struct FDChessboardSquare {
/// From 0...7
public var file: Int
/// From 0...7
public var rank: Int
/// A format like a4
public var algebriac: String { get }
public var index: Int { get set }
public init(index newIndex: Int)
}
/// The pieces on a chess board
public enum FDChessboardPiece : String {
case WhitePawn
case BlackPawn
case WhiteKnight
case BlackKnight
case WhiteBishop
case BlackBishop
case WhiteRook
case BlackRook
case WhiteQueen
case BlackQueen
case WhiteKing
case BlackKing
}
public protocol FDChessboardViewDataSource : class {
/// What piece is on the square?
public func chessboardView(board: FDChessboardView, pieceForSquare square: FDChessboardSquare) -> FDChessboardView.FDChessboardPiece?
/// The last move
public func chessboardViewLastMove(board: FDChessboardView) -> (from: FDChessboardView.FDChessboardSquare, to: FDChessboardView.FDChessboardSquare)?
/// The premove
public func chessboardViewPremove(board: FDChessboardView) -> (from: FDChessboardView.FDChessboardSquare, to: FDChessboardView.FDChessboardSquare)?
}
public protocol FDChessboardViewDelegate : class {
/// Where can this piece move to?
public func chessboardView(board: FDChessboardView, legalDestinationsForPieceAtSquare from: FDChessboardSquare) -> [FDChessboardView.FDChessboardSquare]
/// Before a move happens (cannot be stopped)
public func chessboardView(board: FDChessboardView, willMoveFrom from: FDChessboardSquare, to: FDChessboardSquare)
/// After a move happened
public func chessboardView(board: FDChessboardView, didMoveFrom from: FDChessboardSquare, to: FDChessboardSquare)
/// Before a move happens (cannot be stopped)
public func chessboardView(board: FDChessboardView, willMoveFrom from: FDChessboardSquare, to: FDChessboardSquare, withPromotion promotion: FDChessboardPiece)
/// After a move happened
public func chessboardView(board: FDChessboardView, didMoveFrom from: FDChessboardSquare, to: FDChessboardSquare, withPromotion promotion: FDChessboardPiece)
}
/// Display for a chess board
public class FDChessboardView : UIView {
/// Color for "white" board squares
public var lightBackgroundColor: UIColor
/// Color for "black" board squares
public var darkBackgroundColor: UIColor
/// Color for where a piece is moving to
public var targetBackgroundColor: UIColor
/// Color for a legal square target
public var legalBackgroundColor: UIColor
/// Color for the last move square
public var lastMoveColor: UIColor
/// Color for a premove square
public var premoveColor: UIColor
/// Path for custom piece graphics
public var pieceGraphicsDirectoryPath: String?
/// Datasource to say which pieces are on each square
public weak var dataSource: FDChessboardViewDataSource? { get set }
/// Handler for user interaction with the view
public weak var delegate: FDChessboardViewDelegate?
/// Should piece moves be animated?
public var doesAnimate: Bool
/// Should legal squares be shown when a piece is selected?
public var doesShowLegalSquares: Bool
/// Should the lash move be shown?
public var doesShowLastMove: Bool
/// Should premove be shown?
public var doesShowPremove: Bool
/// UIView initializer
public override init(frame: CGRect)
/// UIView initializer
required public init?(coder aDecoder: NSCoder)
/// Add a piece onto the board
public func setPiece(piece: FDChessboardPiece?, forSquare square: FDChessboardSquare)
/// Repull all board information from data source
public func reloadData()
/// Move a piece on the board
public func movePieceAtCoordinate(from: FDChessboardSquare, toCoordinate to: FDChessboardSquare) -> Bool
/// Move a piece on the board
public func movePieceAtCoordinate(from: FDChessboardSquare, toCoordinate to: FDChessboardSquare, andPromoteTo piece: FDChessboardPiece) -> Bool
/// Premove a piece on the board
public func premovePieceAtCoordinate(from: FDChessboardSquare, toCoordinate to: FDChessboardSquare) -> Bool
/// Premove a piece on the board
public func premovePieceAtCoordinate(from: FDChessboardSquare, toCoordinate to: FDChessboardSquare, andPromoteTo piece: FDChessboardPiece) -> Bool
}
安装
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
需要 CocoaPods 0.39.0+ 才能构建 FDChessboardView 0.1.0+。
要使用 CocoaPods 将 FDChessboardView 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
pod 'FDChessboardView', '~> 0.1'
然后,运行以下命令
$ pod install
Carthage
Carthage 是一个集中式依赖管理器,它构建您的依赖,并提供二进制框架。
您可以使用以下命令通过 Homebrew 安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将FDChessboardView 集成到您的 Xcode 项目中,请在其 Cartfile
中指定它
github "fulldecent/FDChessboardView" ~> 0.1
运行 carthage update
命令以构建框架,并将构建的 FDChessboardView.framework
拖到您的 Xcode 项目中。
即将推出的功能
以下内容在 API 中进行讨论,并等待实现
- 显示最后一步棋的显示
- 可变游戏状态(即可以移动棋子)
- 棋子移动动画
- 在开始拖动后 highlighted 合法方格的棋子
- 预见(Premove)
另见
另请参阅Mac版本的Kibitz,它正在回归。https://github.com/fulldecent/kibitz