From aad626197ac7aa31c7295f36986913df6dbcb88c Mon Sep 17 00:00:00 2001
From: carefree <946715806@qq.com>
Date: Mon, 29 Jun 2020 18:26:35 +0800
Subject: [PATCH] fix callback bug in scrollView
---
.../Base.lproj/Main.storyboard | 151 ++++++++++--------
.../ViewController.swift | 24 ++-
KFGradientProgressView.podspec | 2 +-
README.md | 2 +-
Sources/GradientProgressView.swift | 35 ++--
5 files changed, 114 insertions(+), 100 deletions(-)
diff --git a/GradientProgressViewExample/Base.lproj/Main.storyboard b/GradientProgressViewExample/Base.lproj/Main.storyboard
index 133954a..074ef8a 100644
--- a/GradientProgressViewExample/Base.lproj/Main.storyboard
+++ b/GradientProgressViewExample/Base.lproj/Main.storyboard
@@ -15,80 +15,93 @@
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
diff --git a/GradientProgressViewExample/ViewController.swift b/GradientProgressViewExample/ViewController.swift
index dbd4144..89f9116 100644
--- a/GradientProgressViewExample/ViewController.swift
+++ b/GradientProgressViewExample/ViewController.swift
@@ -62,7 +62,7 @@ class ViewController: UIViewController {
self.progressLabel.text = "\(text)%"
//计算出标签的移动位置
- let newFrame = self.progressView4.convert(frame, to: self.view)
+ let newFrame = self.progressView4.convert(frame, to: self.self.progressView4.superview)
let size: CGFloat = 36
let tagFrame = CGRect(x: (newFrame.origin.x + newFrame.size.width) - size / 2.3, y: newFrame.origin.y - size, width: size, height: size)
self.tagView.frame = tagFrame
@@ -72,7 +72,13 @@ class ViewController: UIViewController {
//设置标签的初始位置
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
- self.resetPosition()
+ let frame = self.progressView4.frame
+ let size: CGFloat = 36
+ let tagFrame = CGRect(x: frame.origin.x - size / 2.3, y: frame.origin.y - size, width: size, height: size)
+ self.tagView.frame = tagFrame
+ self.progressLabel.sizeToFit()
+ self.progressLabel.center = CGPoint(x: self.tagView.center.x, y: self.tagView.center.y - 7)
+ self.progressLabel.text = "0%"
}
}
@@ -81,25 +87,13 @@ class ViewController: UIViewController {
progressView2.progress = 0
progressView3.progress = 0
progressView4.progress = 0
-
- resetPosition()
}
@IBAction func startAction(_ sender: Any) {
progressView1.setProgress(0.2 + progressView1.progress, animated: true)
progressView2.setProgress(0.25 + progressView2.progress, animated: true)
progressView3.setProgress(0.3 + progressView3.progress, animated: true)
- progressView4.setProgress(0.35 + progressView4.progress, animated: true)
- }
-
- func resetPosition() {
- let frame = self.progressView4.frame
- let size: CGFloat = 36
- let tagFrame = CGRect(x: frame.origin.x - size / 2.3, y: frame.origin.y - size, width: size, height: size)
- self.tagView.frame = tagFrame
- self.progressLabel.sizeToFit()
- self.progressLabel.center = CGPoint(x: self.tagView.center.x, y: self.tagView.center.y - 7)
- self.progressLabel.text = "0%"
+ progressView4.setProgress(0.25 + progressView4.progress, animated: true)
}
}
diff --git a/KFGradientProgressView.podspec b/KFGradientProgressView.podspec
index eba83b1..9c250d4 100644
--- a/KFGradientProgressView.podspec
+++ b/KFGradientProgressView.podspec
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "KFGradientProgressView"
- s.version = "1.2.0"
+ s.version = "1.3.0"
s.summary = "一个简单的进度条控件"
s.homepage = "https://github.com/moliya/GradientProgressView"
s.license = "MIT"
diff --git a/README.md b/README.md
index 051f00f..05dfd99 100644
--- a/README.md
+++ b/README.md
@@ -89,7 +89,7 @@ pod 'KFGradientProgressView'
```swift
dependencies: [
- .package(url: "https://github.com/moliya/GradientProgressView", from: "1.2.0")
+ .package(url: "https://github.com/moliya/GradientProgressView", from: "1.3.0")
]
```
diff --git a/Sources/GradientProgressView.swift b/Sources/GradientProgressView.swift
index 61b92ee..9f15d2e 100644
--- a/Sources/GradientProgressView.swift
+++ b/Sources/GradientProgressView.swift
@@ -69,7 +69,6 @@ open class GradientProgressView: UIView {
private var privateProgress: Float = 0
- private var displayLink: CADisplayLink?
private let maskLayer: CALayer = {
let layer = CALayer()
layer.backgroundColor = UIColor.white.cgColor
@@ -121,29 +120,37 @@ open class GradientProgressView: UIView {
}
privateProgress = validProgress
- if privateProgress > 0 {
+ //动画时长
+ var duration = animated ? animationDuration : 0
+ if duration < 0 {
+ duration = 0
+ }
+
+ var displayLink: CADisplayLink?
+ if duration > 0 {
//开启CADisplayLink
displayLink = CADisplayLink(target: self, selector: #selector(displayLinkAction))
- displayLink?.add(to: .main, forMode: .default)
+ //使用common模式,使其在UIScrollView滑动时依然能得到回调
+ displayLink?.add(to: .current, forMode: .common)
}
CATransaction.begin()
- CATransaction.setAnimationDuration(animated ? animationDuration : 0)
+ CATransaction.setAnimationDuration(duration)
CATransaction.setAnimationTimingFunction(timingFunction)
CATransaction.setCompletionBlock {
//停止CADisplayLink
- self.displayLink?.invalidate()
- self.displayLink = nil
- //保证最后的百分比正确
- self.displayLinkAction()
- }
- defer {
- CATransaction.commit()
+ displayLink?.invalidate()
+ if duration == 0 {
+ //更新回调
+ self.progressUpdating?(validProgress, self.maskLayer.frame)
+ }
}
//更新maskLayer的frame
- var bounds = gradientLayer.bounds
- bounds.size.width *= CGFloat(privateProgress)
- maskLayer.frame = bounds
+ var bounds = self.gradientLayer.bounds
+ bounds.size.width *= CGFloat(validProgress)
+ self.maskLayer.frame = bounds
+
+ CATransaction.commit()
}
}