嘿,我在ios12上有一个内置于swift 4.2中的基本相机应用。 我的问题很基本,但我无法解决。
我当前的相机设置。
我有一个UIbutton
,当.touchDown
和.touchUpInside
试图获得按钮按下效果时,它会改变其大小。并添加了双击手势识别器。这两个事件(按下按钮和双击)都执行摄像机侧面从前向后的切换。
我在这里面临两个问题:
1.按下button
切换侧面时,按钮会在.touchDown
事件上执行缩小效果,但会滞后于.touchUpInside
事件,直到事件发生,直到事件发生在相机侧面。翻转。
UIView
,它负责捕获图像。此按钮也具有与上面的按钮类似的变换效果。但是我添加了double tap gesture
,它在单击图像时会产生滞后。它将首先缩小,捕获图像,然后调用.touchupInside
事件,该事件将使缩小的按钮达到其大小。我真的无法理解其原因。我尝试使用DispatchQueues.main.async
,但没有运气。
如何解决。代码如下:
func configureButtonhaptic() {
cameraFlipButton.addTarget(self, action: #selector(camFlipHold), for: .touchDown)
cameraFlipButton.addTarget(self, action: #selector(camFlipRelease), for: .touchUpInside)
}
@objc func camFlipHold() {
cameraFlipButton.transformView(withDuration: 0.1, scaleX: 0.8, y: 0.8)
}
@objc func camFlipRelease() {
lightTapticFeedback()
cameraFlipButton.transformView(withDuration: 0.1, scaleX: 1, y: 1)
toggleCamera()
}
func addDoubleTapForCameraSwitch() {
let tap = UITapGestureRecognizer(target: self, action: #selector(switchCamera))
tap.numberOfTapsRequired = 2
view.addGestureRecognizer(tap)
}
func addCaptureButton() {
let x : CGFloat = self.view.frame.midX - captureButtonSide/2
let y : CGFloat = (self.view.frame.maxY - buttonLowerPadding) - captureButtonSide
SCCaptureButton.frame = CGRect(x: x, y: y, width: captureButtonSide, height: captureButtonSide)
SCCaptureButton.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.3)
SCCaptureButton.makeCircle()
SCCaptureButton.layer.borderWidth = 3
SCCaptureButton.layer.borderColor = UIColor.white.cgColor
SCCaptureButton.addTarget(self, action: #selector(takePhotoOnTap), for: .touchUpInside)
self.view.addSubview(SCCaptureButton)
}
func activateCaptureButton() {
SCCaptureButton.addTarget(self, action: #selector(captureHoldDown), for: .touchDown)
SCCaptureButton.addTarget(self, action: #selector(captureReleased), for: .touchUpInside)
}
@objc func captureHoldDown() {
lightTapticFeedback()
SCCaptureButton.transformView(withDuration: 0.1, scaleX: 0.95, y: 0.95)
}
@objc func captureReleased() {
lightTapticFeedback()
SCCaptureButton.transformView(withDuration: 0.1, scaleX: 1, y: 1)
}