在 UIPinchGestureRecognizer 上禁用缩小
Disable zoom out on UIPinchGestureRecognizer
我想禁用缩小,我已经尝试通过设置最小值在 UIScrollView 上的故事板中执行此操作,但没有任何区别。
let tap = UIPinchGestureRecognizer(target: self, action: #selector(self.pinchHandler(gesture:)))
tap.delegate = self as? UIGestureRecognizerDelegate
self.scrollView.addGestureRecognizer(tap)
@objc private func pinchHandler(gesture: UIPinchGestureRecognizer) {
if let view = gesture.view {
switch gesture.state {
case .changed:
self.navigationController?.isNavigationBarHidden = true
let pinchCenter = CGPoint(x: gesture.location(in: view).x - view.bounds.midX,
y: gesture.location(in: view).y - view.bounds.midY)
let transform = view.transform.translatedBy(x: pinchCenter.x, y: pinchCenter.y)
.scaledBy(x: gesture.scale, y: gesture.scale)
.translatedBy(x: -pinchCenter.x, y: -pinchCenter.y)
view.transform = transform
gesture.scale = 1
case .ended:
UIView.animate(withDuration: 0.2, animations: {
view.transform = CGAffineTransform.identity
self.navigationController?.isNavigationBarHidden = false
})
default:
return
}
}
}
因此,为了重申 Quoc 在评论中提到的内容,您的代码应该有点像这样 -
@objc private func pinchHandler(gesture: UIPinchGestureRecognizer) {
if let view = gesture.view {
switch gesture.state {
case .changed:
if(gesture.scale < 1.0) { return }
//your code
case .ended:
//your code
default:
return
}
}
}
我想禁用缩小,我已经尝试通过设置最小值在 UIScrollView 上的故事板中执行此操作,但没有任何区别。
let tap = UIPinchGestureRecognizer(target: self, action: #selector(self.pinchHandler(gesture:)))
tap.delegate = self as? UIGestureRecognizerDelegate
self.scrollView.addGestureRecognizer(tap)
@objc private func pinchHandler(gesture: UIPinchGestureRecognizer) {
if let view = gesture.view {
switch gesture.state {
case .changed:
self.navigationController?.isNavigationBarHidden = true
let pinchCenter = CGPoint(x: gesture.location(in: view).x - view.bounds.midX,
y: gesture.location(in: view).y - view.bounds.midY)
let transform = view.transform.translatedBy(x: pinchCenter.x, y: pinchCenter.y)
.scaledBy(x: gesture.scale, y: gesture.scale)
.translatedBy(x: -pinchCenter.x, y: -pinchCenter.y)
view.transform = transform
gesture.scale = 1
case .ended:
UIView.animate(withDuration: 0.2, animations: {
view.transform = CGAffineTransform.identity
self.navigationController?.isNavigationBarHidden = false
})
default:
return
}
}
}
因此,为了重申 Quoc 在评论中提到的内容,您的代码应该有点像这样 -
@objc private func pinchHandler(gesture: UIPinchGestureRecognizer) {
if let view = gesture.view {
switch gesture.state {
case .changed:
if(gesture.scale < 1.0) { return }
//your code
case .ended:
//your code
default:
return
}
}
}