私のUIViewController
私はUIView
という単一のサブクラスを持っています。その中で私はティックタックのトウボードを描きます。どういうわけか、私がUIBezierPath()
を使って描画している仕切り(「#」形)は、ボードを均等に分割していません。 1/3-1/3-1/3の代わりに、縦の仕切りは、寸法のプリントアウトが意味をなさないにもかかわらず、45%-45%-10%に近くなります。私は何が欠けていますか?おかげで私のサブクラスでUIViewとしてティックタックトウボードを素早く描画する方法は?
:
import UIKit
@IBDesignable class GameBoardView: UIView {
override func drawRect(rect: CGRect) {
// set up gameBoard dimensions everytime drawRect() is called
setUpGameBoardCells()
self.frame = CGRectMake(gameBoardPosX, gameBoardPosY, gameBoardLength, gameBoardLength)
print("gameBoard.frame: x=\(self.frame.origin.x), y=\(self.frame.origin.y), h=\(self.frame.height), w=\(self.frame.width)\n")
// draw dividers & cells
var divider = UIBezierPath(rect: CGRect(x: cellWidth, y: 0, width: dividerWidth, height: gameBoardLength))
divider.lineWidth = 1
UIColor.orangeColor().setStroke()
divider.stroke()
divider = UIBezierPath(rect: CGRect(x: cellWidth * 2 + dividerWidth, y: 0, width: dividerWidth, height: gameBoardLength))
divider.stroke()
}
}
そして、これは私が任意のサイズの画面を処理するために寸法を設定する方法です:奇妙である何
var screenSize = CGRect()
let screenMargin: CGFloat = 20 // to the edge
var gameBoardIsPortrait = Bool()
var gameBoardLength = CGFloat()
var gameBoardPosX = CGFloat()
var gameBoardPosY = CGFloat()
let cellsPerRow: Int = 3
var cellWidth = CGFloat()
let dividerWidthGuide: CGFloat = 0.02 // guideline % of gameBoardLength
var dividerWidth = CGFloat()
let debugPrint = true
func setUpGameBoardCells() {
screenSize = UIScreen.mainScreen().bounds
// gameBoard is a square
gameBoardIsPortrait = (screenSize.height >= screenSize.width ? true : false)
gameBoardLength = min(screenSize.height, screenSize.width) - screenMargin * 2
gameBoardPosX = (screenSize.width - gameBoardLength)/2
gameBoardPosY = (screenSize.height - gameBoardLength)/2
// want cells & dividers on gameBoard to be whole numbers
dividerWidth = round(gameBoardLength * dividerWidthGuide)
let cellsTotalWidth: Int = Int(gameBoardLength) - Int(dividerWidth) * (cellsPerRow - 1)
let dividerWidthFudge: CGFloat = (cellsTotalWidth % cellsPerRow == 1 ? -1 : (cellsTotalWidth % cellsPerRow == 2 ? 1 : 0))
dividerWidth += dividerWidthFudge
cellWidth = CGFloat((cellsTotalWidth - Int(dividerWidthFudge) * (cellsPerRow - 1))/cellsPerRow)
if debugPrint {
print("setUpCellDivision()->\nscreen: h=\(screenSize.height), w=\(screenSize.width)")
print("gameBoardIsPortrait=\(gameBoardIsPortrait), gameBoardLength=\(gameBoardLength), gameBoardPosX=\(gameBoardPosX), gameBoardPosY=\(gameBoardPosY)")
print("cellWidth=\(cellWidth), dividerWidth=\(dividerWidth)\n")
}
}
はXcodeで、それが右に見えるということです。
しかし、シミュレータではこのように:あなたの制約が正しく設定されていないかのように
こんにちは、ロブ、あなたの応答に感謝します。私はdrawRect()のCGRectMake()を取り除くことで、分け前が適切な場所に現れることを確認します。もし私がまだプログラムのサイズを変更し、ボードを特定の場所に移動したいのであれば、どこにCGRectMake()コールを置くべきですか?私はviewWillAppear()に私のViewControllerで入れてみましたが、それは動作していないようです。ありがとう – rockhammer
他のビューと同じように 'frame'を設定します。自動レイアウトを使用していて、ストーリーボードに追加した場合は、制約のアウトレットを追加するだけで、必要に応じて制約の 'constant'プロパティを調整できます。キーは、自動レイアウトを使用している場合、 'frame'を直接調整しないことです。制約を調整します。 – Rob
制約!もちろん。ありがとう、ロブ、私はちょうどそれを働かせました。 – rockhammer