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

优化扫码动画效果

parent 358f08bd
......@@ -25,7 +25,6 @@ class LBXScanLineAnimation: UIImageView {
if image != nil {
stepAnimation()
}
}
@objc func stepAnimation() {
......@@ -35,37 +34,69 @@ class LBXScanLineAnimation: UIImageView {
guard let image = self.image else { return }
// 重要:取消之前可能存在的延迟调用
NSObject.cancelPreviousPerformRequests(withTarget: self,
selector: #selector(LBXScanLineAnimation.stepAnimation),
object: nil)
var frame: CGRect = animationRect
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
self.frame = frame
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
var frame = self.animationRect
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
self.frame = frame
}, completion: { (_: Bool) in
self.perform(#selector(LBXScanLineAnimation.stepAnimation), with: nil, afterDelay: 0.3)
}, completion: { [weak self] (_: Bool) in
guard let self = self else { return }
// 检查动画是否应该继续
if self.isAnimationing {
self.perform(#selector(LBXScanLineAnimation.stepAnimation),
with: nil,
afterDelay: 0.3)
}
})
}
func stopStepAnimating() {
self.isHidden = true
// 先设置标记为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 {
......@@ -73,7 +104,7 @@ class LBXScanLineAnimation: UIImageView {
}
deinit {
NSObject.cancelPreviousPerformRequests(withTarget: self)
stopStepAnimating()
}
}
......@@ -38,39 +38,74 @@ class LBXScanNetAnimation: UIImageView {
guard let image = self.image else { return }
// 重要:取消之前可能存在的延迟调用
NSObject.cancelPreviousPerformRequests(withTarget: self,
selector: #selector(LBXScanNetAnimation.stepAnimation),
object: nil)
var frame = animationRect
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
self.frame = frame
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
var frame = self.animationRect
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
self.frame = frame
}, completion: { (_: Bool) in
self.perform(#selector(LBXScanNetAnimation.stepAnimation), with: nil, afterDelay: 0.3)
}, completion: { [weak self] (_: Bool) in
guard let self = self else { return }
// 检查动画是否应该继续
if self.isAnimationing {
self.perform(#selector(LBXScanNetAnimation.stepAnimation),
with: nil,
afterDelay: 0.3)
}
})
}
func stopStepAnimating() {
self.isHidden = true
// 先设置标记为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
}
}
}
override open func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NSObject.cancelPreviousPerformRequests(withTarget: self)
stopScan()
}
@objc open func startScan() {
if scanObj == nil {
......@@ -161,11 +167,6 @@ class LBXScanViewController: YHBaseViewController, UIImagePickerControllerDelega
}
}
override open func viewWillDisappear(_ animated: Bool) {
NSObject.cancelPreviousPerformRequests(withTarget: self)
stopScan()
}
open func openPhotoAlbum() {
LBXPermissions.authorizePhotoWith { [weak self] granted in
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