我使用UIPinchGesture
来调整iPhone相机的videoZoomFactor
。我希望它以与我的手指分开的相同速度放大。我在手势识别器处理程序中使用以下代码:
let pinchVelocityDividerFactor: CGFloat = 35
var error: NSError
let device: AVCaptureDevice! = self.deviceInput!.device
do {
try device.lockForConfiguration()
let desiredZoomFactor = device.videoZoomFactor + atan2(pinchRecognizer.velocity, pinchVelocityDividerFactor);
// Check if desiredZoomFactor fits required range from 1.0 to activeFormat.videoMaxZoomFactor
device.videoZoomFactor = max(1, min(desiredZoomFactor, device.activeFormat.videoMaxZoomFactor))
device.unlockForConfiguration()
}
catch let error1 as NSError {
error = error1
print(error)
}
目前,费率依赖于pinchVelocityDividerFactor。我该怎么调整呢?
答案 0 :(得分:2)
我发现手指移动后的距离与前一次移动距离之间的比例需要加到videoZoomFactor上。这个比例也需要乘以缩放系数,以便在放大时保持缩放(否则,当你进行大量放大时,你将不得不捏很多)。代码如下。
func handlePinchToZoomRecognizer(pinchRecognizer: UIPinchGestureRecognizer) {
if pinchRecognizer.state == .Began {
if (pinchRecognizer.numberOfTouches() >= 2) {
oldFinger1 = pinchRecognizer.locationOfTouch(0, inView: previewView)
oldFinger2 = pinchRecognizer.locationOfTouch(1, inView: previewView)
oldDistance = hypot(oldFinger1.x - oldFinger2.x, oldFinger1.y - oldFinger2.y)
}
}
else if (pinchRecognizer.state == .Began || pinchRecognizer.state == .Changed) {
if (pinchRecognizer.numberOfTouches() >= 2) {
let newFinger1 = pinchRecognizer.locationOfTouch(0, inView: previewView)
let newFinger2 = pinchRecognizer.locationOfTouch(1, inView: previewView)
let distance = hypot(newFinger1.x - newFinger2.x, newFinger1.y - newFinger2.y)
var error: NSError
let device: AVCaptureDevice! = self.deviceInput!.device
do {
try device.lockForConfiguration()
let desiredZoomFactor = device.videoZoomFactor + device.videoZoomFactor*(distance/oldDistance - 1)
print(device.videoZoomFactor*(distance/oldDistance - 1))
// Check if desiredZoomFactor fits required range from 1.0 to activeFormat.videoMaxZoomFactor
device.videoZoomFactor = max(1, min(desiredZoomFactor, device.activeFormat.videoMaxZoomFactor))
oldFinger1 = newFinger1
oldFinger2 = newFinger2
oldDistance = distance
device.unlockForConfiguration()
}
catch let error1 as NSError {
error = error1
print(error)
}
}
}
}