Commit 84f3b544 authored by Alex朱枝文's avatar Alex朱枝文

优化扫码动画效果

parent 358f08bd
...@@ -25,7 +25,6 @@ class LBXScanLineAnimation: UIImageView { ...@@ -25,7 +25,6 @@ class LBXScanLineAnimation: UIImageView {
if image != nil { if image != nil {
stepAnimation() stepAnimation()
} }
} }
@objc func stepAnimation() { @objc func stepAnimation() {
...@@ -35,37 +34,69 @@ class LBXScanLineAnimation: UIImageView { ...@@ -35,37 +34,69 @@ class LBXScanLineAnimation: UIImageView {
guard let image = self.image else { return } guard let image = self.image else { return }
// 重要:取消之前可能存在的延迟调用
NSObject.cancelPreviousPerformRequests(withTarget: self,
selector: #selector(LBXScanLineAnimation.stepAnimation),
object: nil)
var frame: CGRect = animationRect var frame: CGRect = animationRect
let hImg = image.size.height * animationRect.size.width / image.size.width let hImg = image.size.height * animationRect.size.width / image.size.width
frame.origin.y -= hImg // 重置到起始位置
frame.origin.y = animationRect.origin.y
frame.size.height = hImg frame.size.height = hImg
self.frame = frame self.frame = frame
self.alpha = 0.0 self.alpha = 0.0
UIView.animate(withDuration: 1.4, animations: { () in UIView.animate(withDuration: 1.4, animations: { [weak self] () in
guard let self = self else { return }
self.alpha = 1.0 self.alpha = 1.0
var frame = self.animationRect var frame = self.animationRect
let hImg = image.size.height * self.animationRect.size.width / image.size.width let hImg = image.size.height * self.animationRect.size.width / image.size.width
frame.origin.y += (frame.size.height - hImg) frame.origin.y = self.animationRect.origin.y + (self.animationRect.size.height - hImg)
frame.size.height = hImg frame.size.height = hImg
self.frame = frame self.frame = frame
}, completion: { (_: Bool) in }, completion: { [weak self] (_: Bool) in
guard let self = self else { return }
self.perform(#selector(LBXScanLineAnimation.stepAnimation), with: nil, afterDelay: 0.3)
// 检查动画是否应该继续
if self.isAnimationing {
self.perform(#selector(LBXScanLineAnimation.stepAnimation),
with: nil,
afterDelay: 0.3)
}
}) })
} }
func stopStepAnimating() { func stopStepAnimating() {
self.isHidden = true // 先设置标记为false,阻止新的动画开始
isAnimationing = false isAnimationing = false
// 取消所有延迟调用
NSObject.cancelPreviousPerformRequests(withTarget: self)
// 停止当前的动画
self.layer.removeAllAnimations()
// 隐藏视图
self.isHidden = true
// 重置frame到初始位置
if !animationRect.isEmpty {
var frame = animationRect
if let image = self.image {
let hImg = image.size.height * animationRect.size.width / image.size.width
frame.origin.y = animationRect.origin.y
frame.size.height = hImg
}
self.frame = frame
}
} }
static public func instance() -> LBXScanLineAnimation { static public func instance() -> LBXScanLineAnimation {
...@@ -73,7 +104,7 @@ class LBXScanLineAnimation: UIImageView { ...@@ -73,7 +104,7 @@ class LBXScanLineAnimation: UIImageView {
} }
deinit { deinit {
NSObject.cancelPreviousPerformRequests(withTarget: self)
stopStepAnimating() stopStepAnimating()
} }
} }
...@@ -38,39 +38,74 @@ class LBXScanNetAnimation: UIImageView { ...@@ -38,39 +38,74 @@ class LBXScanNetAnimation: UIImageView {
guard let image = self.image else { return } guard let image = self.image else { return }
// 重要:取消之前可能存在的延迟调用
NSObject.cancelPreviousPerformRequests(withTarget: self,
selector: #selector(LBXScanNetAnimation.stepAnimation),
object: nil)
var frame = animationRect var frame = animationRect
let hImg = image.size.height * animationRect.size.width / image.size.width let hImg = image.size.height * animationRect.size.width / image.size.width
frame.origin.y -= hImg // 重置到起始位置
frame.origin.y = animationRect.origin.y
frame.size.height = hImg frame.size.height = hImg
self.frame = frame self.frame = frame
self.alpha = 0.0 self.alpha = 0.0
UIView.animate(withDuration: 1.2, animations: { () in UIView.animate(withDuration: 1.2, animations: { [weak self] () in
guard let self = self else { return }
self.alpha = 1.0 self.alpha = 1.0
var frame = self.animationRect var frame = self.animationRect
let hImg = image.size.height * self.animationRect.size.width / image.size.width let hImg = image.size.height * self.animationRect.size.width / image.size.width
frame.origin.y += (frame.size.height - hImg) frame.origin.y = self.animationRect.origin.y + (self.animationRect.size.height - hImg)
frame.size.height = hImg frame.size.height = hImg
self.frame = frame self.frame = frame
}, completion: { (_: Bool) in }, completion: { [weak self] (_: Bool) in
guard let self = self else { return }
self.perform(#selector(LBXScanNetAnimation.stepAnimation), with: nil, afterDelay: 0.3)
// 检查动画是否应该继续
if self.isAnimationing {
self.perform(#selector(LBXScanNetAnimation.stepAnimation),
with: nil,
afterDelay: 0.3)
}
}) })
} }
func stopStepAnimating() { func stopStepAnimating() {
self.isHidden = true // 先设置标记为false,阻止新的动画开始
isAnimationing = false isAnimationing = false
// 取消所有延迟调用
NSObject.cancelPreviousPerformRequests(withTarget: self)
// 停止当前的动画
self.layer.removeAllAnimations()
// 隐藏视图
self.isHidden = true
// 重置frame到初始位置
if !animationRect.isEmpty {
var frame = animationRect
if let image = self.image {
let hImg = image.size.height * animationRect.size.width / image.size.width
frame.origin.y = animationRect.origin.y
frame.size.height = hImg
}
self.frame = frame
}
}
deinit {
NSObject.cancelPreviousPerformRequests(withTarget: self)
stopStepAnimating()
} }
} }
...@@ -80,6 +80,12 @@ class LBXScanViewController: YHBaseViewController, UIImagePickerControllerDelega ...@@ -80,6 +80,12 @@ class LBXScanViewController: YHBaseViewController, UIImagePickerControllerDelega
} }
} }
} }
override open func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NSObject.cancelPreviousPerformRequests(withTarget: self)
stopScan()
}
@objc open func startScan() { @objc open func startScan() {
if scanObj == nil { if scanObj == nil {
...@@ -161,11 +167,6 @@ class LBXScanViewController: YHBaseViewController, UIImagePickerControllerDelega ...@@ -161,11 +167,6 @@ class LBXScanViewController: YHBaseViewController, UIImagePickerControllerDelega
} }
} }
override open func viewWillDisappear(_ animated: Bool) {
NSObject.cancelPreviousPerformRequests(withTarget: self)
stopScan()
}
open func openPhotoAlbum() { open func openPhotoAlbum() {
LBXPermissions.authorizePhotoWith { [weak self] granted in LBXPermissions.authorizePhotoWith { [weak self] granted in
if granted { if granted {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment