2016-09-01 2 views
0

저는 일반적으로 프로그래밍에 익숙하지 않습니다. 복잡한 레이아웃에 수많은 단추를 만드는 방법을 알아 내려고하고 있습니다. 내 레이아웃은 기본적으로 약 24 섹션으로 분할 된 몸체의 투명한 PNG이며 본문의 각 세그먼트가 별도의 단추가되기를 원합니다.복잡한 이미지 기반 단추 레이아웃

보기 컨트롤러에서 몇 가지 레이아웃을 시도했습니다. 이미지를 오버레이하는 톤을 설정하면 (시뮬레이터에서 시작할 때 레이아웃을 똑바로 유지할 수 없음) 버튼 이미지를 제공하려고 시도했지만 큰 이미지 크기의 버튼을 시도했지만 최상위 버튼 만 사용할 수있었습니다 .

이 작업을 수행 할 수있는 방법이 있습니까 아니면 실행 가능하도록 코드가 필요합니까?

답변

0

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 
    } 

} 

셀에 터치 동작을 추가하려는 경우.

+0

나는이 코드를 알아 내려고하고 있는데 이미지를 가져 와서 4 사분면으로 나눕니다. – cortlandune

+0

죄송합니다. 입력하기 전에 실수로 보내 셨습니다 ...이 코드가 내 상황에서 어떻게 도움이되는지 확신 할 수 없으며 매우 불규칙한 (인체 윤곽선으로 표시된) 모양이 있습니다. 정사각형이 아닙니다. 이것을 사용하여 정확한 점 또는 벡터 모양을 버튼 또는 PNG 모양으로 지정할 수 있습니까? 당신이 올린 코드가 내 머리 위로가는 길일 수도 있습니다. 나는 이것을 이해할 수 없을 것이라고 걱정합니다. 또는이 방법을 사용하여 이미지를 80 개 블록으로 나누고 하나의 버튼의 일부로 세트를 지정하는 것이 가장 효율적입니까? 나는 이미지를 게시 하겠지만 나는 할 수 없다. – cortlandune