-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGalleryVC.swift
More file actions
178 lines (123 loc) · 5.74 KB
/
GalleryVC.swift
File metadata and controls
178 lines (123 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// GalleryVC.swift
// Dindr
//
// Created by lsecrease on 10/5/15.
// Copyright (c) 2015 ImagineME. All rights reserved.
//
import UIKit
import Parse
class GalleryVC: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout,
UIScrollViewDelegate {
/* Views */
@IBOutlet var galleryCollView: UICollectionView!
@IBOutlet var imagePreviewView: UIView!
@IBOutlet var imgScrollView: UIScrollView!
@IBOutlet var imgPrev: UIImageView!
/* Variables */
var galleryArray = NSMutableArray()
var collViewCells: CGFloat = 3.0
var hudView = UIView(frame: CGRectMake(0, 0, 80, 80))
var indicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 80, 80))
override func viewDidLoad() {
super.viewDidLoad()
imagePreviewView.frame = CGRectMake(0, 0, 0, 0)
queryGallery()
//println("\(galleryArray.count)")
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imgPrev
}
func queryGallery() {
// ERROR ALERT VIEW
let errorAlert = UIAlertView(title: "Dindr",
message: "Something went wrong, try again later or check your internet connection",
delegate: nil,
cancelButtonTitle: "OK" )
view.showHUD(view)
galleryArray.removeAllObjects()
let query = PFQuery(className: "Gallery")
query.findObjectsInBackgroundWithBlock { (objects, error)-> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
self.galleryArray.addObject(object)
} }
// Reload Data
self.galleryCollView.reloadData()
self.view.hideHUD()
print("\(self.galleryArray.count)")
} else { errorAlert.show(); self.view.hideHUD() }
}
}
/* MARK: - COLLECTION VIEW DELEGATES ========================*/
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return galleryArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("GalleryCell", forIndexPath: indexPath) as! GalleryCell
var galleryClass = PFObject(className: "Gallery")
galleryClass = galleryArray[indexPath.row] as! PFObject
// Get image
let imageFile = galleryClass["image"] as? PFFile
imageFile?.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
cell.galImage.image = UIImage(data:imageData)
cell.galImage.layer.borderColor = UIColor.blackColor().CGColor
cell.galImage.layer.borderWidth = 1
} } }
//
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(view.frame.size.width / collViewCells, view.frame.size.width / collViewCells)
}
// IMAGE TAPPED
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var galleryClass = PFObject(className: "Gallery")
galleryClass = galleryArray[indexPath.row] as! PFObject
let imageFile = galleryClass["image"] as? PFFile
imageFile?.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.imgPrev.image = UIImage(data:imageData)
self.showImagePrevView()
} } }
}
// SHOW/HIDE PREVIEW IMAGE VIEW
func showImagePrevView() {
UIView.animateWithDuration(0.1, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: {
self.imagePreviewView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
}, completion: { (finished: Bool) in })
}
func hideImagePrevView() {
imgPrev.image = nil
UIView.animateWithDuration(0.1, delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: {
self.imagePreviewView.frame = CGRectMake(0, 0, 0, 0)
}, completion: { (finished: Bool) in })
}
// TAP ON IMAGE TO CLOSE PREVIEW
@IBAction func tapToClosePreview(sender: UITapGestureRecognizer) {
hideImagePrevView()
}
}
extension UIView {
func showHUD(inView: UIView) {
hudView.center = CGPointMake(inView.frame.size.width/2, inView.frame.size.height/2)
hudView.backgroundColor = UIColor(red: 237.0/255.0, green: 85.0/255.0, blue: 100.0/255.0, alpha: 1.0)
hudView.alpha = 0.9
hudView.layer.cornerRadius = hudView.bounds.size.width/2
indicatorView.center = CGPointMake(hudView.frame.size.width/2, hudView.frame.size.height/2)
indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
hudView.addSubview(indicatorView)
inView.addSubview(hudView)
indicatorView.startAnimating()
}
func hideHUD() { hudView.removeFromSuperview() }
}