gpt4 book ai didi

ios - 平移手势(按住/拖动)像 Snapchat 一样在相机上缩放

转载 作者:搜寻专家 更新时间:2023-11-01 06:54:35 25 4
gpt4 key购买 nike

我正在尝试复制 Snapchat 相机的缩放功能,一旦您开始录制,您可以向上或向下拖动手指,它会相应地放大或缩小。我已经成功地缩放了捏,但一直坚持使用 PanGestureRecognizer 进行缩放。

这是我试过的代码,问题是我不知道如何替换用于捏合手势识别器缩放的 sender.scale。我正在使用 AVFoundation。基本上,我问的是如何像在 TikTok 或 Snapchat 中一样正确地进行按住缩放(一根手指拖动)。

let minimumZoom: CGFloat = 1.0
let maximumZoom: CGFloat = 15.0
var lastZoomFactor: CGFloat = 1.0
var latestDirection: Int = 0

@objc func panGesture(_ sender: UIPanGestureRecognizer) {

let velocity = sender.velocity(in: doubleTapSwitchCamButton)
var currentDirection: Int = 0

if velocity.y > 0 || velocity.y < 0 {

let originalCapSession = captureSession
var devitce : AVCaptureDevice!

let videoDeviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .builtInDuoCamera], mediaType: AVMediaType.video, position: .unspecified)
let devices = videoDeviceDiscoverySession.devices
devitce = devices.first!

guard let device = devitce else { return }

// Return zoom value between the minimum and maximum zoom values
func minMaxZoom(_ factor: CGFloat) -> CGFloat {
return min(min(max(factor, minimumZoom), maximumZoom), device.activeFormat.videoMaxZoomFactor)
}

func update(scale factor: CGFloat) {
do {

try device.lockForConfiguration()
defer { device.unlockForConfiguration() }
device.videoZoomFactor = factor
} catch {
print("\(error.localizedDescription)")
}
}

//These 2 lines below are the problematic ones, pinch zoom uses this one below, and the newScaleFactor below that is a testing one that did not work.
let newScaleFactor = minMaxZoom(sender.scale * lastZoomFactor)
//let newScaleFactor = CGFloat(exactly: number + lastZoomFactor)


switch sender.state {

case .began: fallthrough
case .changed: update(scale: newScaleFactor!)
case .ended:
lastZoomFactor = minMaxZoom(newScaleFactor!)
update(scale: lastZoomFactor)

default: break
}

} else {

}

latestDirection = currentDirection

}

最佳答案

您可以使用手势识别器的平移属性,以点为单位计算位移,并将此位移归一化为缩放因子。

将此放入您的代码中,您可以尝试:

 ... somewhere in your view setup code, i.e. viewDidLoad.... 
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panGesture))
button.addGestureRecognizer(panGestureRecognizer)

private var initialZoom: CGFloat = 1.0
@objc func panGesture(_ sender: UIPanGestureRecognizer) {

// note that 'view' here is the overall video preview
let velocity = sender.velocity(in: view)

if velocity.y > 0 || velocity.y < 0 {

let originalCapSession = captureSession
var devitce : AVCaptureDevice!

let videoDeviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .builtInDuoCamera], mediaType: AVMediaType.video, position: .unspecified)
let devices = videoDeviceDiscoverySession.devices
devitce = devices.first!

guard let device = devitce else { return }

let minimumZoomFactor: CGFloat = 1.0
let maximumZoomFactor: CGFloat = min(device.activeFormat.videoMaxZoomFactor, 10.0) // artificially set a max useable zoom of 10x

// clamp a zoom factor between minimumZoom and maximumZoom
func clampZoomFactor(_ factor: CGFloat) -> CGFloat {
return min(max(factor, minimumZoomFactor), maximumZoomFactor)
}

func update(scale factor: CGFloat) {
do {

try device.lockForConfiguration()
defer { device.unlockForConfiguration() }
device.videoZoomFactor = factor
} catch {
print("\(error.localizedDescription)")
}
}

switch sender.state {

case .began:
initialZoom = device.videoZoomFactor
startRecording() /// call to start recording your video

case .changed:

// distance in points for the full zoom range (e.g. min to max), could be view.frame.height
let fullRangeDistancePoints: CGFloat = 300.0

// extract current distance travelled, from gesture start
let currentYTranslation: CGFloat = sender.translation(in: view).y

// calculate a normalized zoom factor between [-1,1], where up is positive (ie zooming in)
let normalizedZoomFactor = -1 * max(-1,min(1,currentYTranslation / fullRangeDistancePoints))

// calculate effective zoom scale to use
let newZoomFactor = clampZoomFactor(initialZoom + normalizedZoomFactor * (maximumZoomFactor - minimumZoomFactor))

// update device's zoom factor'
update(scale: newZoomFactor)

case .ended, .cancelled:
stopRecording() /// call to start recording your video
break

default:
break
}
}
}

关于ios - 平移手势(按住/拖动)像 Snapchat 一样在相机上缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54431439/

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