-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainViewController.swift
226 lines (184 loc) · 9.39 KB
/
MainViewController.swift
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import UIKit
import Alamofire
import SwiftyJSON
import Kingfisher
import MJRefresh
class ThemeList {
var id:String!
var keyword:String?
var smallUrl:String?
var fullsizeUrl:String?
var width:Int?
var height:Int?
var color:String!
var downloads:Int!
var likes:Int!
}
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate,PECropViewControllerDelegate {
//@IBOutlet weak var tableViews: UITableView!
let Url : String = "http://133.130.125.179:8080/unsplashs/"
let key : String = "29a307bccd8555abb09dcd36bbc3e014bad09e75e18c18ae369eb338b2a01f4a"
let perPage:String = "5"
var themeLists:[ThemeList] = []
var selectedRow:Int?
var page:Int = 1
var controller=PECropViewController()
var tumblrHUD:AMTumblrHud=AMTumblrHud()
@IBOutlet weak var tableViews: UITableView!
let refresh:UIRefreshControl=UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().statusBarStyle = .LightContent
let height=UIDevice.deviceScreenHeight/2-10
let width=UIDevice.deviceScreenWidth/2-26
self.tumblrHUD=AMTumblrHud(frame: CGRectMake(width, height, 55, 20))
self.tumblrHUD.hudColor=UIColor.UIColorFromRGB(0xF1F2F3)
self.view.addSubview(tumblrHUD)
tumblrHUD.hidden=true
tableViews.delegate = self
tableViews.dataSource = self
setBackground(page)
self.setNavigationBar()
// 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
let header:MJRefreshGifHeader = MJRefreshGifHeader(refreshingTarget: self, refreshingAction: "loadNewData")
// 设置自动切换透明度(在导航栏下面自动隐藏)
header.automaticallyChangeAlpha = false
// 隐藏时间
header.lastUpdatedTimeLabel?.hidden=true
// 设置header
self.tableViews.mj_header = header
// 设置footer
self.tableViews.mj_footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: "loadNewData")
}
func loadNewData(){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
// 刷新表格
self.tableViews.reloadData()
// 拿到当前的下拉刷新控件,结束刷新状态
self.tableViews.mj_header.endRefreshing()
}
}
//设置toolbar点击事件
func setNavigationBar(){
self.navigationController?.navigationBar.setBackgroundImage(UIDevice.imageWithColor(UIColor.UIColorFromRGB(0xFFB212).colorWithAlphaComponent(1)) , forBarMetrics: UIBarMetrics.Default)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.themeLists.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// let width = CGFloat(self.themeLists[indexPath.row].width!)
// let height = CGFloat(self.themeLists[indexPath.row].height!)
//
// let newWidth = self.view.bounds.width
// let newHeight = (height / width) * newWidth
// if height > 0 {
// return CGFloat(newHeight)
// } else {
// return 200.0
// }
return 220.0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
if let backgroundUrl = self.themeLists[indexPath.row].smallUrl {
let imageView = NSBundle.mainBundle().loadNibNamed("ImageList", owner: self, options: nil)[0] as! ImageList
imageView.frame = cell.bounds
let placeholder=UIDevice.imageWithColor(UIColor.UIColorFromRGB(0xFFFFFF).colorWithAlphaComponent(1))
imageView.imageViewDetail.kf_setImageWithURL(NSURL(string: backgroundUrl)!, placeholderImage: placeholder, optionsInfo: [.Transition(ImageTransition.Fade(0.2))], completionHandler: { action in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
})
imageView.tag=2
cell.addSubview(imageView)
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
tumblrHUD.hidden=false
tumblrHUD.showAnimated(true)
if let backgroundUrl = self.themeLists[indexPath.row].fullsizeUrl {
let images=UIImageView()
images.frame=CGRect(x: 0, y: 0, width: 1242, height:1242)
images.kf_setImageWithURL(NSURL(string: backgroundUrl)!, placeholderImage: nil, optionsInfo: [.Transition(ImageTransition.Fade(0.2))], completionHandler: { action in
self.tumblrHUD.hidden=true
self.tumblrHUD.showAnimated(false)
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
self.openEditor(images)
})
}
}
func openEditor(images:UIImageView){
self.controller=PECropViewController()
self.controller.delegate=self
self.controller.image=images.image
self.controller.cropAspectRatio=640.0/1136.0
self.controller.keepingCropAspectRatio=false
let navigationController=UINavigationController(rootViewController: self.controller)
self.presentViewController(navigationController, animated: false, completion: nil)
}
func cropViewController(controller: PECropViewController!, didFinishCroppingImage croppedImage: UIImage!, btn button: UIButton!) {
if button.tag==200{
print("save")
let imageData=UIImagePNGRepresentation(croppedImage)
UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData!)!, nil, nil, nil)
let alertView=UIAlertView(title: "", message: "保存成功", delegate: self, cancelButtonTitle: "取消")
alertView.alertViewStyle = .Default
alertView.addButtonWithTitle("打开相册")
alertView.show()
}else{
print("look")
let lockScreenController = PreviewViewController(image: croppedImage)
lockScreenController.modalPresentationStyle=UIModalPresentationStyle.FormSheet
controller.presentViewController(lockScreenController, animated: false, completion: nil)
}
}
func cropViewController(controller: PECropViewController!, didFinishCroppingImage croppedImage: UIImage!) {
}
func cropViewControllerDidCancel(controller: PECropViewController!) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
case 1:
UIApplication.sharedApplication().openURL(NSURL(string: "photos-redirect://")!)
default:
print("a")
}
}
func setBackground(page: Int) {
tumblrHUD.hidden=false
tumblrHUD.showAnimated(true)
var jsonData:JSON?
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
Alamofire.request(.GET, self.Url+String(self.page), parameters: nil)
.responseJSON(completionHandler: { req, _, result in
print(req)
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if result.value != nil {
jsonData = JSON(result.value!)
for (_, subJson):(String, JSON) in jsonData! {
let themeList:ThemeList = ThemeList()
themeList.id = subJson["id"].stringValue
themeList.smallUrl = subJson["fullurl"].stringValue+"-thumb"
themeList.fullsizeUrl = subJson["fullurl"].stringValue+"-full"
print(themeList.fullsizeUrl)
self.themeLists.append(themeList)
self.tableViews.insertRowsAtIndexPaths([NSIndexPath(forRow: self.themeLists.count-1, inSection: 0)], withRowAnimation: UITableViewRowAnimation.None)
self.tableViews.reloadRowsAtIndexPaths([NSIndexPath(forRow: self.themeLists.count-1, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Left)
}
self.tumblrHUD.hidden=true
self.tumblrHUD.showAnimated(false)
}
})
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height
if bottomEdge >= scrollView.contentSize.height {
setBackground(++page)
}
}
}