//
// progressView.swift
// swift-AFNetworking
//
// Created by HMC on 16/8/11.
// Copyright © 2016年 SKing. All rights reserved.
//
import UIKit
class progressView: UIView {
var progress : CGFloat = 0.0 {
//属性改变监听
didSet {
print("改变了")
setNeedsDisplay()
}
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
backgroundColor = UIColor.clearColor()
let arcCenter = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
let radius = rect.width * 0.5 - 3
let startAngle = CGFloat(-M_PI_2)
let endAngle = CGFloat(M_PI * 2) * progress + startAngle
let bezierPath = UIBezierPath(arcCenter: arcCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
bezierPath.addLineToPoint(arcCenter)
bezierPath.closePath()
UIColor(white: 1.0, alpha: 0.8).setFill()
bezierPath.fill()
}
}
控件的设置,调用方法如下,点击加载按钮 下载图片显示进度.
效果图
//
// ViewController.swift
// swift-AFNetworking
//
// Created by HMC on 16/8/9.
// Copyright © 2016年 SKing. All rights reserved.
//
import UIKit
import HYLabel
import SDWebImage
class ViewController: UIViewController {
@IBAction func GoToAuth(sender: AnyObject) {
let authNav = UINavigationController(rootViewController: AuthViewController())
presentViewController(authNav, animated: true, completion: nil)
}
@IBOutlet weak var imageView: UIImageView!
@IBAction func loadView(sender: AnyObject) {
imageView.sd_setImageWithURL(NSURL(string:"http://httpbin.org/image/jpeg"), placeholderImage: nil, options: [], progress: { (current, total) in
self.progress.hidden = false
self.progress.progress = CGFloat(current) / CGFloat(total)
}) { (_, _, _, _) in
self.progress.hidden = true
}
SDImageCache.sharedImageCache().clearDisk()
}
//懒加载
lazy var progress : progressView = progressView()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.greenColor()
view.addSubview(progress)
progress.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
progress.center = CGPoint(x: SCREENWIDTH * 0.5, y: SCREENHEIGHT * 0.5)
progress.hidden = true
}
}
网友评论