// // ExploreCollectionReusableView.swift // librx // // Created by Yunlong Zheng on 15/9/6. // Copyright (c) 2015年 librx. All rights reserved. // import UIKit import SDWebImage class ExploreCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { private var screenSize: CGRect! private var screenWidth: CGFloat! private var screenHeight: CGFloat! private var explores: [Explore] = [] private var loginAction :LoginAction? private var loginStateChange: LoginStateHandler? private var searchController:UISearchController! private var colors: [String] = ["#FB7F5A", "#1276EF", "#525C89", "#FED51D"] weak var delegate: ExploreCollectionViewController? override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] self.navigationController?.navigationBar.backgroundColor = UIColor.clearColor() screenSize = UIScreen.mainScreen().bounds screenWidth = screenSize.width screenHeight = screenSize.height } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.navigationController?.navigationBarHidden = false self.navigationController?.setNavigationBarHidden(false, animated: false) LibrxService.loadExplore { (explores) -> () in self.explores = explores self.collectionView?.reloadData() } } override func prefersStatusBarHidden() -> Bool { return false } override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return explores.count } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(15, 15, 15, 15); } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { //return CGSizeMake(150.0, 100.0); return CGSize(width: screenWidth/2-20, height: screenWidth/2-20); } override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ExploreCell // mark cell content let explore: Explore = explores[indexPath.row] cell.nameLabel.text = "\(explore.name)" cell.nameLabel.textColor = UIColor.whiteColor() cell.nameLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping cell.nameLabel.numberOfLines = 2 cell.iconImage.subviews.forEach({ $0.removeFromSuperview() }) cell.iconImage.sd_setImageWithURL(NSURL(string: explore.iconUrl), placeholderImage: UIImage(named:"AppIcon")) let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = cell.iconImage.bounds blurEffectView.alpha = 0.6 cell.iconImage.addSubview(blurEffectView) cell.backgroundColor = UIColor(hexString: self.colors[indexPath.row]) cell.layer.cornerRadius = 1 cell.clipsToBounds = true return cell } func dismiss() { dismissViewControllerAnimated(true, completion: nil) } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showExploreSegue" { if let indexPaths = self.collectionView?.indexPathsForSelectedItems() { let selectedExplore = self.explores[indexPaths[0].row] let destinationController = segue.destinationViewController as! ExploreTableViewController destinationController.title = selectedExplore.name print("\(selectedExplore.resourceUrl)") destinationController.dataUrl = selectedExplore.resourceUrl } } } }