2016-08-26 61 views
6

我有uicollectionview代碼CustomCollectionViewLayout,裏面有a lot of small cells但用戶不能看到他們沒有zoom。也可以選擇所有單元格。如何放大UICollectionView

我想添加我的收藏視圖裏面的縮放功能!

我的清晰代碼如下。

class CustomCollectionViewController: UICollectionViewController { 

    var items = [Item]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 


     customCollectionViewLayout.delegate = self 

     getDataFromServer() 

} 

func getDataFromServer() { 

     HttpManager.getRequest(url, parameter: .None) { [weak self] (responseData, errorMessage) ->() in 

      guard let strongSelf = self else { return } 

      guard let responseData = responseData else { 
       print("Get request error \(errorMessage)") 
       return 
      } 

      guard let customCollectionViewLayout = strongSelf.collectionView?.collectionViewLayout as? CustomCollectionViewLayout else { return } 

      strongSelf.items = responseData 
      customCollectionViewLayout.dataSourceDidUpdate = true 

      NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in 
       strongSelf.collectionView!.reloadData() 
      }) 
     } 
    } 
} 

extension CustomCollectionViewController { 

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
     return items.count 
    } 

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return items[section].services.count + 1 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell 


      cell.label.text = items[indexPath.section].base 


     return cell 

} 

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath cellForItemAtIndexPath: NSIndexPath) { 

    print(items[cellForItemAtIndexPath.section].base) 


} 

} 

此外,根據下面你可以看到有我的UICollectionView佈局屬性我選擇maxZoom 4doesnt有任何動作!

enter image description here

謝謝!

+0

這個問題可能會有所幫助:[製作的CollectionView縮放(http://stackoverflow.com/questions/13485617/make-uicollectionview-zoomable) – jojoT

+0

@jojoT TY但樣品OBJ-C和做雙指縮放每個項目我想要快速和區域縮放不是每個項目簡單縮放 – SwiftDeveloper

+0

是用戶觸摸集合視圖,然後單元格變焦?你的意思是,觸摸後,整個收集視圖變得模糊,觸及單元格只出現在可視寬度和高度的屏幕中心? –

回答

1

您不會像放大簡單的滾動視圖那樣縮放集合。相反,您應該添加捏合手勢(或其他縮放機制)並使用它來更改佈局,以便網格在集合的可見部分顯示不同數量的項目。這基本上改變了列的數量,從而改變了項目大小(單元大小)。當您更新佈局時,集合可以在不同尺寸之間進行動畫製作,但您希望獲得平滑縮放的可能性非常小,您希望它在一個步驟中從N列直接到N-1列。

+0

謝謝,但我知道這一點。我想要將我的UICollectionView集成到簡單的ZOOM中,點擊裏面會放大。我需要的代碼不需要想法:) – SwiftDeveloper

1

我想你問的看起來像什麼在題爲先進的集散觀與構建自定義佈局(演示開始於20:20)https://www.youtube.com/watch?v=8vB2TMS2uhE

你基本上要pinchGesture添加到您的WWDC1012視頻做UICollectionView,然後將捏特性(比例,中心)傳遞給UICollectionViewLayout(它是UICollectionViewFlowLayout的子類),然後您的佈局將執行放大所需單元格所需的轉換。

+0

我看過但不解決我的問題 – SwiftDeveloper