UICollectionViewController
またはUICollectionView
の方が適しています。 UICollectionView
の背景イメージを設定して、セルをボタンと見なすことができます。
ここは簡単な例です。
import UIKit
class Collection: UICollectionViewController {
private let cellId = "cell"
override init(collectionViewLayout layout: UICollectionViewLayout) {
if let l = layout as? UICollectionViewFlowLayout{
l.minimumInteritemSpacing = 0
l.sectionInset = UIEdgeInsetsZero
l.scrollDirection = .Vertical
l.footerReferenceSize = CGSize.zero
l.headerReferenceSize = CGSize.zero
let screenWidth = UIScreen.mainScreen().bounds.width
let itemWidth = CGFloat(Int(screenWidth/4))
l.itemSize = CGSize(width: itemWidth, height: itemWidth)
}
super.init(collectionViewLayout: layout)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor()
collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
}
}
extension Collection{
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 4
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)
cell.backgroundColor = UIColor.redColor()
return cell
}
}
セルにタッチアクションを追加する場合。
私はこのコードを理解しようとしています。イメージを取り、それを4象限に分割していると言いますか? – cortlandune
申し訳ありませんが、入力を終える前に間違って送信しました...私の状況でこのコードがどのように役立つかは不明ですが、非常に不規則な(人体の輪郭を描いた)形があります。それほど正方形ではない。正確な点やベクトル図形をボタンやPNG図形として指定する方法はありますか?あなたが投稿したコードが私の頭の上に行くかもしれない。私はこれを理解することができないと心配している。または、これを使用して画像を80個のブロックに分割し、1つのボタンの一部としてセットを指定するのが最も効率的でしょうか?私は画像を投稿するが、私はできない。 – cortlandune