프로젝트 하면서 Gradient ProgressBar가 필요해서 Github에서 참조해서 커스텀 했다.
import Foundation
public class IGradientProgressBarView : UIProgressView {
// MARK: - Properties
/// An array of CGColorRef objects defining the color of each gradient stop. Animatable.
public var gradientColors: [CGColor] = [#colorLiteral(red: 0.9486141801, green: 0.7027528882, blue: 0.1464758813, alpha: 1).cgColor, #colorLiteral(red: 0.955791533, green: 0.4503515363, blue: 0.6127513647, alpha: 1).cgColor] {
didSet {
gradientLayer.colors = gradientColors
}
}
/** The radius to use when drawing rounded corners for the gradient layer and progress view backgrounds. Animatable.
* The default value of this property is 0.0.
*/
@IBInspectable public var cornerRadius: CGFloat = 0 {
didSet {
self.layer.cornerRadius = cornerRadius
self.clipsToBounds = true
}
}
public override var trackTintColor: UIColor? {
didSet {
if trackTintColor != UIColor.clear {
backgroundColor = trackTintColor
trackTintColor = UIColor.clear
}
}
}
public override var progressTintColor: UIColor? {
didSet {
if progressTintColor != UIColor.clear {
progressTintColor = UIColor.clear
}
}
}
lazy private var gradientLayer: CAGradientLayer = self.initGradientLayer()
// MARK: - init methods
override public init (frame : CGRect) {
super.init(frame : frame)
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
// MARK: Layout
override public func layoutSubviews() {
super.layoutSubviews()
updateGradientLayer()
}
override public func setProgress(_ progress: Float, animated: Bool) {
super.setProgress(progress, animated: animated)
updateGradientLayer()
}
// MARK: - Private methods
private func setup() {
self.layer.cornerRadius = cornerRadius
self.layer.addSublayer(gradientLayer)
progressTintColor = UIColor.clear
gradientLayer.colors = gradientColors
}
private func initGradientLayer() -> CAGradientLayer {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = sizeByPercentage(originalRect: bounds, width: CGFloat(progress))
gradientLayer.anchorPoint = CGPoint(x: 0, y: 0)
gradientLayer.position = CGPoint(x: 0, y: 0)
gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
gradientLayer.cornerRadius = cornerRadius
gradientLayer.masksToBounds = true
return gradientLayer
}
private func updateGradientLayer() {
gradientLayer.frame = sizeByPercentage(originalRect: bounds, width: CGFloat(progress))
gradientLayer.cornerRadius = cornerRadius
}
private func sizeByPercentage(originalRect: CGRect, width: CGFloat) -> CGRect {
let newSize = CGSize(width: originalRect.width * width, height: originalRect.height)
return CGRect(origin: originalRect.origin, size: newSize)
}
}
[사용방법]
import UIKit
import Alamofire
class DownloadViewController: UIViewController {
@IBOutlet weak var downloadProgressBar: IGradientProgressBarView!
override func viewDidLoad() {
super.viewDidLoad()
downloadProgressBar.gradientColors = [UIColor.squash.cgColor, UIColor.rosyPink.cgColor]
downloadProgressBar.progress = 0.0
}
func fileDownlaodRequest(downloadUrl: String, bundle: String, complete: @escaping (Bool) -> Void) {
let destination: DownloadRequest.Destination = {[weak self] _, _ in
var fileURL: URL?
if let filePath = BundleFileManager.shared.filePath {
fileURL = filePath.appendingPathComponent(bundle)
print("다운로드 파일 URL : \(fileURL?.absoluteString)")
}
return (fileURL!, [])
}
AF.download(downloadUrl, to: destination)
.downloadProgress { [weak self] progress in
self?.downloadProgressBar.progress = Float(progress.fractionCompleted)
}
.response { [weak self] response in
if response.error == nil {
complete(true)
} else {
complete(false)
}
}
}
}
'Swift > UIView' 카테고리의 다른 글
[SWIFT]hitTest 중첩뷰 아래 있는 뷰 컨트롤 하기 (0) | 2023.06.15 |
---|---|
[SWIFT]여러개의 중첩된 뷰의 위치 변경 (0) | 2023.06.15 |
[SWIFT]커스텀 숫자키패드 한줄배열 및 두줄배열/ UITextField 커스텀 키보드 (0) | 2023.06.09 |
[SWIFT]UIView 상단 Gradient 효과 주기(채팅창 상단) (0) | 2023.06.09 |
[SWIFT]subView 레이어 순서 변경 (0) | 2023.06.09 |