Swift 为什么我的手势识别器返回零?
我曾尝试结合大量教程,尝试让手势识别器在mapView上收到手势后在tableView上启动动画(移动地图后缩小表格,就像旧的uber应用程序一样)。动画功能,但是一旦动画完成,我会收到“打开可选包装时意外发现为零”-但是它似乎不应该为零,显然是,我只是不明白如何。错误率下降了3/4,我试图减少尽可能多的代码,以使部分丢失。我的猜测是,我已经创建了两个手势识别器,这是导致问题的原因,但我不确定如何组合它们。代码如下:Swift 为什么我的手势识别器返回零?,swift,uipangesturerecognizer,Swift,Uipangesturerecognizer,我曾尝试结合大量教程,尝试让手势识别器在mapView上收到手势后在tableView上启动动画(移动地图后缩小表格,就像旧的uber应用程序一样)。动画功能,但是一旦动画完成,我会收到“打开可选包装时意外发现为零”-但是它似乎不应该为零,显然是,我只是不明白如何。错误率下降了3/4,我试图减少尽可能多的代码,以使部分丢失。我的猜测是,我已经创建了两个手势识别器,这是导致问题的原因,但我不确定如何组合它们。代码如下: class RestaurantsVC: UIViewControll
class RestaurantsVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIGestureRecognizerDelegate {
var animator: UIViewPropertyAnimator?
var currentState: AnimationState!
var thumbnailFrame: CGRect!
var panGestureRecognizer: UIPanGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(RestaurantsVC.handlePan(gestureRecognizer:)))
panGestureRecognizer.delegate = self
self.mapView.addGestureRecognizer(panGestureRecognizer)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@objc func handlePan (gestureRecognizer: UIPanGestureRecognizer) {
let translation = gestureRecognizer.translation(in: self.view.superview)
switch gestureRecognizer.state {
case .began:
startPanning()
animator?.startAnimation()
case .ended:
let velocity = gestureRecognizer.velocity(in: self.view.superview)
endAnimation(translation: translation, velocity: velocity)
default:
print("Something went wrong handlePan")
}
}
func startPanning() {
var finalFrame:CGRect = CGRect()
switch currentState {
// code
}
animator = UIViewPropertyAnimator(duration: 1, dampingRatio: 0.8, animations: {
})
}
func endAnimation (translation:CGPoint, velocity:CGPoint) {
if let animator = self.animator {
self.panGestureRecognizer.isEnabled = false //(this line is where i get the error)//
switch self.currentState {
case .thumbnail:
animator.isReversed = false
animator.addCompletion({ _ in
self.currentState = .minimized
self.panGestureRecognizer.isEnabled = true
})
case .minimized:
animator.isReversed = true
animator.addCompletion({ _ in
self.currentState = .thumbnail
self.panGestureRecognizer.isEnabled = true
})
default:
print("unknown state")
}
}
}
}
它是
nil
,因为您从未实际分配它
在viewDidLoad()
函数中,您实际上是在重新定义一个局部变量,该局部变量会遮挡实例变量:
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(RestaurantsVC.handlePan(gestureRecognizer:)))
^ The 'let' keyword is redefining this variable at the local scope
您应该删除let
声明,并将其指定为:
self.panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(RestaurantsVC.handlePan(gestureRecognizer:)))
这可以在不存储对手势识别器的引用的情况下仍然工作。您可以将手势识别器中的值传递到
handlePan
中的startAnimation
和endAnimation
。这将减少对类中多余属性的需求这太快了!非常感谢你,我被困在这个问题上很长时间了,想不到。不用担心!这是一个非常常见的问题,我建议查看您的Xcode项目构建设置,看看是否可以打开编译器警告/错误以防止这些类型的错误。我想可能是这样的,谢谢您的提示,现在就可以了。谢谢,我的代码是各种教程和堆栈溢出文章的组合,一旦添加了所有功能,我需要坐下来编辑它,这应该给我一个很好的起点。