您的位置:首页 > 移动开发 > Swift

swift 使用MAGearRefreshControl控件刷新

2015-07-17 15:27 176 查看
class ThridViewController:UIViewController, UITableViewDataSource,UITableViewDelegate,MAGearRefreshDelegate {

var _tableView:UITableView!
var refreshControlView:MAGearRefreshControl!
var isLoading =false

overridefunc viewDidLoad() {
super.viewDidLoad();

self.navigationController?.navigationBar.translucent = false

var screeSize:CGSize =UIScreen.mainScreen().bounds.size

_tableView =UITableView(frame: CGRectMake(0,0, screeSize.width, screeSize.height), style: .Plain)
_tableView.dataSource =self
_tableView.delegate =self
self.view.addSubview(_tableView)

refreshControlView =MAGearRefreshControl(frame: CGRectMake(0, -_tableView.bounds.height,self.view.frame.width,_tableView.bounds.height))
refreshControlView.backgroundColor = UIColor.initRGB(234, g:234, b: 234)

refreshControlView.addInitialGear(nbTeeth:12, color:UIColor.initRGB(40, g:40, b: 40), radius:16)
refreshControlView.addLinkedGear(0, nbTeeth:16, color: UIColor.initRGB(80, g:80, b: 80).colorWithAlphaComponent(0.8), angleInDegree:30)
refreshControlView.addLinkedGear(0, nbTeeth:32, color: UIColor.initRGB(104, g:104, b: 104).colorWithAlphaComponent(0.4), angleInDegree:190)
refreshControlView.addLinkedGear(1, nbTeeth:40, color: UIColor.initRGB(104, g:104, b: 104).colorWithAlphaComponent(0.4), angleInDegree: -30)
refreshControlView.addLinkedGear(2, nbTeeth:24, color: UIColor.initRGB(80, g:80, b: 80).colorWithAlphaComponent(0.8), angleInDegree: -190)
refreshControlView.addLinkedGear(3, nbTeeth:10, color: UIColor.initRGB(80, g:80, b: 80), angleInDegree:40)
refreshControlView.setMainGearPhase(0)
refreshControlView.delegate =self
refreshControlView.barColor =UIColor.initRGB(200, g:200, b: 200)
_tableView.addSubview(refreshControlView)

}

func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return20;
}

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
var identiferString ="Cell_\(indexPath.row)"

var cell :UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(identiferString)as? UITableViewCell
if(cell ==nil){
cell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier: identiferString)
}

cell?.textLabel?.text ="Row \(indexPath.row)"

return cell!;
}

//MARK: - UIScrollViewDelegate protocol conformance

func scrollViewDidScroll(scrollView:UIScrollView) {
refreshControlView.MAGearRefreshScrollViewDidScroll(scrollView)
}

func scrollViewDidEndDragging(scrollView:UIScrollView, willDecelerate decelerate:Bool) {
refreshControlView.MAGearRefreshScrollViewDidEndDragging(scrollView)
}

overridefunc didRotateFromInterfaceOrientation(fromInterfaceOrientation:UIInterfaceOrientation) {
refreshControlView.frame =CGRectMake(0, -_tableView.bounds.height,self.view.frame.size.width,_tableView.bounds.size.height)
}

//MARK: - MAGearRefreshDelegate protocol conformance

func MAGearRefreshTableHeaderDataSourceIsLoading(view:MAGearRefreshControl) -> Bool {
returnisLoading
}

func MAGearRefreshTableHeaderDidTriggerRefresh(view:MAGearRefreshControl) {
refresh()
}

func refresh(){
isLoading =true

// -- DO SOMETHING AWESOME (... or just wait 3 seconds) --
// This is where you'll make requests to an API, reload data, or process information
var delayInSeconds =0.6
var popTime =dispatch_time(DISPATCH_TIME_NOW,Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(popTime,dispatch_get_main_queue()) { () ->Void in
// When done requesting/reloading/processing invoke endRefreshing, to close the control
self.isLoading =false
self.refreshControlView.MAGearRefreshScrollViewDataSourceDidFinishedLoading(self._tableView)

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: