-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZYVisionCameraFocusView.swift
More file actions
112 lines (87 loc) · 3.61 KB
/
ZYVisionCameraFocusView.swift
File metadata and controls
112 lines (87 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// PigeonCameraFocusView.swift
// PigeonScan
//
// Created by zy on 2021/7/29.
//
import Foundation
import UIKit
public protocol ZYVisionCameraCustomFocus : NSObjectProtocol {
/// focus view
var zyvision_focousView: ZYVisionCameraFocusView! { get set }
/// 对焦view
var zyvision_focusPreviewView: UIView! { get }
/// 配置
func zyvision_setupFocusView()
/// 用户点击了屏幕, 要对焦 聚焦
func cameraFocusOnTapPreviewView(tapGes: UITapGestureRecognizer)
/// 用户在放大缩小 要调焦
func cameraFocusOnPinchPreviewView(pinchGes: UIPinchGestureRecognizer)
var tmpzoom: CGFloat { get set }
var videozoom: CGFloat { get set }
}
public extension ZYVisionCameraCustomFocus {
func zyvision_setupFocusView() {
self.zyvision_focousView = ZYVisionCameraFocusView(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
let tapGes = UITapGestureRecognizer(taps: 1, touches: 1) { tapges in
if tapges.state == .ended {
let point = tapges.location(in: self.zyvision_focusPreviewView)
self.zyvision_focusPreviewView.addSubview(self.zyvision_focousView)
self.zyvision_focousView.showInCenter(point: point)
self.cameraFocusOnTapPreviewView(tapGes: tapges)
}
}
self.zyvision_focusPreviewView.addGestureRecognizer(tapGes)
let zoomGes = UIPinchGestureRecognizer { pinges in
self.cameraFocusOnPinchPreviewView(pinchGes: pinges)
}
self.zyvision_focusPreviewView.addGestureRecognizer(zoomGes)
self.tmpzoom = 0
self.videozoom = 0
}
}
public class ZYVisionCameraFocusView: UIView {
var shapeLayer: CAShapeLayer = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
self.layer.addSublayer(self.shapeLayer)
self.shapeLayer.strokeColor = UIColor(zyvision_hexString: "#FFBD27").cgColor
self.shapeLayer.lineWidth = 1.0
self.shapeLayer.fillColor = CGColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.0)
self.backgroundColor = .clear
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func layoutSubviews() {
super.layoutSubviews()
self.shapeLayer.frame = self.bounds
self.shapeLayer.path = UIBezierPath(rect: self.bounds).cgPath
let cenx = self.bounds.size.width / 2
let ceny = self.bounds.size.height / 2
_ = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height)
let path = UIBezierPath()
path.move(to: CGPoint(x: cenx/2, y: ceny))
path.addLine(to: CGPoint(x: 3 * cenx / 2, y: ceny))
path.move(to: CGPoint(x: cenx, y: ceny/2))
path.addLine(to: CGPoint(x: cenx, y: 3 * ceny / 2))
path.append(UIBezierPath(rect: self.bounds))
self.shapeLayer.path = path.cgPath
}
func showInCenter(point: CGPoint) {
self.center = point
self.alpha = 0
UIView.animate(withDuration: 0.1) {
self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
} completion: { s in
UIView.animate(withDuration: 0.25) {
self.transform = .identity
self.alpha = 1
} completion: { s in
UIView.animate(withDuration: 0.5) {
self.alpha = 0
}
}
}
}
}