gpt4 book ai didi

ios - 如何以相同的捏合速度缩放 videoZoomFactor

转载 作者:可可西里 更新时间:2023-11-01 01:37:24 24 4
gpt4 key购买 nike

我正在使用 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。我该如何调整?

最佳答案

我发现手指移动后手指之间的距离与前一次移动的距离之比需要添加到 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)
}
}
}
}

关于ios - 如何以相同的捏合速度缩放 videoZoomFactor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35321052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com