Created
March 12, 2015 16:56
-
-
Save ericcgu/a957110ed4216e7b02b5 to your computer and use it in GitHub Desktop.
PFQueryCollectionView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import Parse | |
| class myGalleryVC: PFQueryCollectionViewController, UISearchBarDelegate { | |
| var clockRefresh = clockRefreshControl(frame: CGRectNull) | |
| var gestureRecognizer = UIGestureRecognizer() | |
| var isSearching = false | |
| var searchText: String? | |
| var isRefreshing = false | |
| var originalOffset = CGPoint() | |
| var lastContentOffset: CGFloat = 0.0 | |
| required init(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| self.parseClassName = "userPhoto" | |
| // self.pullToRefreshEnabled = true | |
| // self.paginationEnabled = true | |
| self.objectsPerPage = 20 | |
| } | |
| override func viewDidLoad() { | |
| var layout = self.collectionViewLayout as UICollectionViewFlowLayout | |
| layout.sectionInset = UIEdgeInsetsMake(0.0, 5.0, 0, 5.0) | |
| layout.minimumInteritemSpacing = 0 | |
| initRefreshControl() | |
| // view.addGestureRecognizer(self.slidingViewController().panGesture) | |
| self.navigationController?.navigationBar.addGestureRecognizer(self.slidingViewController().panGesture) | |
| self.slidingViewController().topViewAnchoredGesture = ECSlidingViewControllerAnchoredGesture.Tapping | ECSlidingViewControllerAnchoredGesture.Panning | |
| self.loadObjects() | |
| } | |
| override func viewWillAppear(animated: Bool) { | |
| var layout = self.collectionViewLayout as UICollectionViewFlowLayout | |
| layout.invalidateLayout() | |
| clockRefresh = clockRefreshControl(frame: CGRectMake(view.frame.width/2 - 67/2, -70, view.frame.width, 70)) | |
| navigationController?.view.addGestureRecognizer(slidingViewController().panGesture) | |
| } | |
| override func viewDidAppear(animated: Bool) { | |
| originalOffset = collectionView!.contentOffset | |
| } | |
| override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) { | |
| var layout = self.collectionViewLayout as UICollectionViewFlowLayout | |
| layout.invalidateLayout() | |
| clockRefresh = clockRefreshControl(frame: CGRectMake(view.frame.width/2 - 67/2, -70, view.frame.width, 70)) | |
| } | |
| func initRefreshControl() { | |
| clockRefresh = clockRefreshControl(frame: CGRectMake(view.frame.width/2 - 67/2, -70, view.frame.width, 70)) | |
| collectionView?.addSubview(clockRefresh) | |
| } | |
| override func scrollViewDidScroll(scrollView: UIScrollView) { | |
| println(scrollView.contentOffset.y) | |
| var contentOffset = scrollView.contentOffset.y | |
| if isRefreshing { | |
| collectionView!.bounces = true | |
| } | |
| if contentOffset < -clockRefresh.frame.height && !isRefreshing { | |
| if lastContentOffset > collectionView?.contentOffset.y { | |
| clockRefresh.advanceBy += 6 | |
| } else if lastContentOffset < collectionView?.contentOffset.y { | |
| clockRefresh.advanceBy += -6 | |
| } | |
| lastContentOffset = collectionView!.contentOffset.y | |
| } | |
| view.endEditing(true) | |
| } | |
| override func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool { | |
| if isRefreshing { | |
| return false | |
| } | |
| return true | |
| } | |
| override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { | |
| var contentOffset = scrollView.contentOffset.y | |
| if contentOffset < -180 && !isRefreshing && decelerate { | |
| startRefreshing() | |
| } | |
| } | |
| func startRefreshing() { | |
| println("Started refresh") | |
| isRefreshing = true | |
| collectionView!.bounces = false | |
| self.collectionView!.setContentOffset(CGPointMake(0, -140), animated: true) | |
| clockRefresh.startAnimatingTime() | |
| loadObjects() | |
| } | |
| func stopRefreshing(sender: AnyObject?) { | |
| collectionView?.setContentOffset(originalOffset, animated: true) | |
| collectionView?.bounces = true | |
| println("Ended refresh") | |
| clockRefresh.stopAnimatingTime() | |
| isRefreshing = false | |
| } | |
| override func queryForCollection() -> PFQuery! { | |
| var query = super.queryForCollection() | |
| query.orderByDescending("createdAt"); | |
| query.whereKey("isLatest", equalTo: true) | |
| if (PFUser.currentUser() != nil) { | |
| query.orderByDescending("createdAt"); | |
| query.whereKey("isLatest", equalTo: true) | |
| query.whereKey("user", equalTo: PFUser.currentUser()) | |
| if isSearching { | |
| query.whereKey("imageDescription", containsString: searchText!) | |
| isSearching = false | |
| searchText = nil | |
| } | |
| } else { | |
| query.whereKey("user", equalTo: "") | |
| println("User is nil") | |
| } | |
| return query | |
| } | |
| override func objectsDidLoad(error: NSError!) { | |
| super.objectsDidLoad(error) | |
| if error != nil { | |
| println(error.localizedDescription) | |
| } | |
| } | |
| func searchBarSearchButtonClicked(searchBar: UISearchBar) { | |
| searchText = searchBar.text | |
| loadObjects() | |
| searchBar.resignFirstResponder() | |
| } | |
| func searchBarTextDidBeginEditing(searchBar: UISearchBar) { | |
| println("didstart") | |
| gestureRecognizer = UITapGestureRecognizer(target: self, action: "resignResponder:") | |
| view.addGestureRecognizer(gestureRecognizer) | |
| } | |
| func resignResponder(sender: AnyObject?) { | |
| view.removeGestureRecognizer(gestureRecognizer) | |
| view.endEditing(true) | |
| } | |
| override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFCollectionViewCell! { | |
| let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as? customCollectionViewCell | |
| cell?.myImageView.file = objects[indexPath.row]["thumbnailFile"] as PFFile | |
| cell?.myImageView.contentMode = UIViewContentMode.ScaleAspectFill | |
| cell?.myImageView.loadInBackground({ (image:UIImage!, error:NSError!) -> Void in | |
| if error == nil { | |
| cell?.myImageView.image = image | |
| } else { | |
| println(error); | |
| } | |
| }) | |
| return cell | |
| } | |
| override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { | |
| //super.collectionView(collectionView, viewForSupplementaryElementOfKind: kind, atIndexPath: indexPath) | |
| var reusableView: UICollectionReusableView! | |
| if kind == UICollectionElementKindSectionHeader { | |
| let headerView: searchBarCell = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "searchBar", forIndexPath: indexPath) as searchBarCell | |
| headerView.searchBar.delegate = self | |
| reusableView = headerView | |
| } | |
| return reusableView | |
| } | |
| override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { | |
| if indexPath.row % 15 == 0 && indexPath.row > 0 { | |
| println("Loading next page \(indexPath.row)") | |
| if !loading { | |
| loadNextPage() | |
| } | |
| } | |
| } | |
| override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { | |
| var destVC = storyboard?.instantiateViewControllerWithIdentifier("imageViewer") as imageViewerViewController; | |
| let imageAtIndexPath = objectAtIndexPath(indexPath) as PFObject | |
| destVC.imageObject = imageAtIndexPath | |
| (imageAtIndexPath["thumbnailFile"] as PFFile).getDataInBackgroundWithBlock { (data: NSData!, error: NSError!) -> Void in | |
| destVC.image = UIImage(data: data) | |
| self.navigationController?.pushViewController(destVC, animated: true) | |
| } | |
| } | |
| override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { | |
| var size = CGRectMake(0, 0, view.frame.width/2 - 7.5, view.frame.width/2 - 5).size | |
| if (view.frame.width > 414){ | |
| size = CGRectMake(0, 0, view.frame.width/3 - 7.5, view.frame.width/3 - 5).size | |
| } | |
| return size | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment