标签:gray scroll white log var insets ati mat ade
1.运行效果图
2.swift 代码的实现
import UIKit
class RefreshWebViewController: UIViewController,UIScrollViewDelegate,UIWebViewDelegate{
@IBOutlet weak var webView: UIWebView!
//刷新控件view
private var header:UIView!
//下拉到50时松开刷新
private let pullHeight:CGFloat=50
//刷新提示文字
private var headerText:UILabel!
//刷新指示器(旋转小圆圈)
private var activityView:UIActivityIndicatorView!
//标记刷新状态
private var isRefreshing=false
override func viewDidLoad() {
super.viewDidLoad()
self.webView.scrollView.delegate=self
self.webView.delegate=self
self.automaticallyAdjustsScrollViewInsets=false
initHeaderView()
loading()
}
//定义下拉刷新控件
private func initHeaderView( ) {
self.header=UIView(frame: CGRectMake(0,0, self.view.frame.width,200))
self.header.backgroundColor=UIColor.darkGrayColor()
self.headerText=UILabel(frame: CGRectMake((self.view.frame.width-120)/2,10,150,30))
self.headerText.text="下拉刷新"
self.headerText.textAlignment=NSTextAlignment.Center
self.headerText.textColor=UIColor.whiteColor()
self.header.addSubview(headerText)
self.activityView=UIActivityIndicatorView(frame: CGRectMake( self.headerText.frame.origin.x-5,15,20,20))
self.header.addSubview(activityView)
self.webView.insertSubview(header, atIndex: 0)
}
//加载网页
func loading( ) {
self.webView.loadRequest(NSURLRequest(URL: NSURL(string:"http://www.hao123.com")!))
}
//scroll滚动时调用
func scrollViewDidScroll(scrollView: UIScrollView) {
if isRefreshing{
return
}
//当下拉到pullHeight高度时松开即可刷新
if scrollView.contentOffset.y <= -pullHeight{
self.headerText.text="松开立即刷新"
}else{
self.headerText.text="下拉刷新"
}
}
// scrollview停止滚动时调用
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if isRefreshing{
return
}
if scrollView.contentOffset.y <= -pullHeight{
refreshing()
}
}
//开始刷新
func refreshing( ) {
isRefreshing=true
self.headerText.text="正在刷新..."
self.activityView.startAnimating()
UIView.animateWithDuration(0.5, delay: 0, options: .CurveEaseOut, animations: {
// 设置刷新控件停留
var offY = self.webView.scrollView.contentInset
offY.top += self.pullHeight
self.webView.scrollView.contentInset=offY
}, completion:{(_)->Void in
})
self.loading()
}
//刷新结束
func refreshEnd() {
self.headerText.text="刷新成功"
self.activityView.stopAnimating()
UIView.animateWithDuration(1, delay: 0, options: .CurveEaseOut, animations: {
//恢复刷新控件到原始位置
var offY = self.webView.scrollView.contentInset
offY.top -= self.pullHeight
self.webView.scrollView.contentInset=offY
}, completion:{(_)->Void in
self.isRefreshing=false
self.headerText.text="下拉刷新"
})
}
//网页加载完成
func webViewDidFinishLoad(webView: UIWebView) {
if isRefreshing{
refreshEnd()
}
}
}
标签:gray scroll white log var insets ati mat ade
原文地址:http://www.cnblogs.com/zzjbk/p/6071064.html