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 개 블록으로 나누고 하나의 버튼의 일부로 세트를 지정하는 것이 가장 효율적입니까? 나는 이미지를 게시 하겠지만 나는 할 수 없다. – cortlandune