Custom CollectionView like as Facebook Feed for iOS
- iOS 8.0+
- Xcode 8+
- Swift 3
To integrate PhotoCollectionView into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'PhotoCollectionView', '~> 1.5.2'
Then, run the following command:
$ pod install
- Drag and drop
Sources
folder into your project
Use can create instance of PhotoCollectionView
by code:
import PhotoCollectionView
let photoCollectionView = PhotoCollectionView(frame: CGRect(x: 0, y: 0, width: 320, height: 240))
or create an UIView
in storyboard and Outlet it
import PhotoCollectionView
@IBOutlet weak var imageCollectionView: PhotoCollectionView!
- Extension
PhotoCollectionViewDataSource
for binding data
extension ViewController: PhotoCollectionViewDataSource {
func numPhotos(in photoCollectionView: PhotoCollectionView) -> Int {
// Return count photo.
return 10
}
func photoCollectionView(_ photoCollectionView: PhotoCollectionView, photoSource index: Int) -> PhotoSource {
return .url(URL(string: urls[index]))
// or return .image(*your image*)
}
}
- Set
dataSource
of PhotoCollectionView
imageCollectionView.dataSource = self
Handle image selection in PhotoCollectionView
imageCollectionView.delegate = self
extension ViewController: PhotoCollectionViewDelegate {
func photoCollectionView(_ photoCollectionView: PhotoCollectionView, didSelectImageAt index: Int) {
print("Press at index \(index)")
}
}
Luan Tran - noblakit01@gmail.com