Skip to content

Commit

Permalink
Add fade animation when taking photos.
Browse files Browse the repository at this point in the history
  • Loading branch information
longitachi committed Apr 24, 2022
1 parent fca9302 commit 6587641
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
21 changes: 10 additions & 11 deletions Sources/Camera/ZLCustomCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ open class ZLCustomCamera: UIViewController, CAAnimationDelegate {

var recordLongGes: UILongPressGestureRecognizer?

/// 是否正在调整焦距
var isAdjustingFocusPoint = false

// 仅支持竖屏
public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
Expand Down Expand Up @@ -209,11 +212,7 @@ open class ZLCustomCamera: UIViewController, CAAnimationDelegate {
#endif
} else if self.cameraConfigureFinish, self.viewDidAppearCount == 0 {
self.showTipsLabel(animate: true)
let animation = CABasicAnimation(keyPath: "opacity")
animation.toValue = 1
animation.duration = 0.15
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
let animation = getFadeAnimation(fromValue: 0, toValue: 1, duration: 0.15)
self.previewLayer?.add(animation, forKey: nil)
self.setFocusCusor(point: self.view.center)
}
Expand Down Expand Up @@ -743,7 +742,7 @@ open class ZLCustomCamera: UIViewController, CAAnimationDelegate {

// 调整焦点
@objc func adjustFocusPoint(_ tap: UITapGestureRecognizer) {
guard self.session.isRunning else {
guard session.isRunning, !isAdjustingFocusPoint else {
return
}
let point = tap.location(in: self.view)
Expand All @@ -754,13 +753,15 @@ open class ZLCustomCamera: UIViewController, CAAnimationDelegate {
}

func setFocusCusor(point: CGPoint) {
isAdjustingFocusPoint = true
self.focusCursorView.center = point
self.focusCursorView.layer.removeAllAnimations()
self.focusCursorView.alpha = 1
self.focusCursorView.layer.transform = CATransform3DMakeScale(1.2, 1.2, 1)
UIView.animate(withDuration: 0.5, animations: {
self.focusCursorView.layer.transform = CATransform3DIdentity
}) { (_) in
self.isAdjustingFocusPoint = false
self.focusCursorView.alpha = 0
}
// ui坐标转换为摄像头坐标
Expand Down Expand Up @@ -957,11 +958,9 @@ open class ZLCustomCamera: UIViewController, CAAnimationDelegate {
extension ZLCustomCamera: AVCapturePhotoCaptureDelegate {

public func photoOutput(_ output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings) {
DispatchQueue.main.async {
self.previewLayer?.opacity = 0
UIView.animate(withDuration: 0.25) {
self.previewLayer?.opacity = 1
}
ZLMainAsync {
let animation = getFadeAnimation(fromValue: 0, toValue: 1, duration: 0.25)
self.previewLayer?.add(animation, forKey: nil)
}
}

Expand Down
10 changes: 10 additions & 0 deletions Sources/General/ZLGeneralDefine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ func getSpringAnimation() -> CAKeyframeAnimation {
return animate
}

func getFadeAnimation(fromValue: CGFloat, toValue: CGFloat, duration: TimeInterval) -> CAAnimation {
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue = fromValue
animation.toValue = toValue
animation.duration = duration
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
return animation
}

func showAlertView(_ message: String, _ sender: UIViewController?) {
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: localLanguageTextValue(.ok), style: .default, handler: nil)
Expand Down

0 comments on commit 6587641

Please sign in to comment.