我们用纯代码实现一下瀑布流:
AppDelegate:
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: (UIScreen.mainScreen().bounds))
window?.rootViewController = WaterFlowViewController()
window?.makeKeyAndVisible()
return true
}
- 搞一个随机色:
extension UIColor {
//随机颜色
class func randomColor() ->UIColor {
return UIColor(red: randomValue(), green: randomValue(), blue: randomValue(), alpha: 1)
}
class func randomValue()->CGFloat {
return CGFloat(arc4random_uniform(256))/255
}
}
- 自定义布局:
protocol WaterFlowViewLayoutDelegate:NSObjectProtocol
{
//collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
///width是瀑布流每列的宽度
func waterFlowViewLayout(waterFlowViewLayout:WaterFlowViewLayout,heightForWidth:CGFloat,atIndextPath:NSIndexPath)->CGFloat
}
class WaterFlowViewLayout: UICollectionViewLayout {
weak