使用 SwiftyCams didFocusAtPoint 函数创建点击聚焦动画

Create animation for tap to focus using SwiftyCams didFocusAtPoint function

我正在使用 SwiftyCam,并希望在使用点击聚焦功能时提供某种动画反馈。

SwiftyCam 带有一个 didFocusAtPoint 函数,当点击屏幕时调用该函数并为您提供点击点。

我现在如何创建和显示动画?

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFocusAtPoint point: CGPoint) {
     // Called when a user initiates a tap gesture on the preview layer
     // Will only be called if tapToFocus = true
     // Returns a CGPoint of the tap location on the preview layer
}

Github 上的 DemoSwiftyCam 项目已经实现了此功能:

ViewController.swift

func swiftyCam(_ swiftyCam: SwiftyCamViewController, didFocusAtPoint point: CGPoint) {
    print("Did focus at point: \(point)")
    focusAnimationAt(point)
}

...

extension ViewController {

///...


fileprivate func focusAnimationAt(_ point: CGPoint) {
    let focusView = UIImageView(image: #imageLiteral(resourceName: "focus")) // Image Available in DemoSwiftyCam Assets.xcassets
    focusView.center = point
    focusView.alpha = 0.0
    view.addSubview(focusView)

    UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: {
        focusView.alpha = 1.0
        focusView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
    }) { (success) in
        UIView.animate(withDuration: 0.15, delay: 0.5, options: .curveEaseInOut, animations: {
            focusView.alpha = 0.0
            focusView.transform = CGAffineTransform(translationX: 0.6, y: 0.6)
        }) { (success) in
            focusView.removeFromSuperview()
        }
    }
}