我有一個UIScrollView
和UIImageView
這樣我就可以使用pinche放大圖像視圖中:如何將平移手勢從UIScrollView轉發到UIImageView?
extension CropperViewController : UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.imageView;
}
}
我現在也希望能夠自由移動UIImageView
所以我嘗試添加一個UIPanGestureRecognizer to my
UIImageView`:
self.imageView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(_:))));
func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view);
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y);
gestureRecognizer.setTranslation(CGPoint.zero, in: self.view);
}
}
我現在有一個問題,就是根本沒有觸發pan touch事件,所以我想UIScrollView
可能會捕獲所有這些事件。因此,#2一些研究告訴我,以下內容添加到我的UIScrollView
:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)));
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.cancelsTouchesInView = false;
self.scrollView.addGestureRecognizer(tapGestureRecognizer);
,但實際上是什麼也沒有改變。縮放後,我可以縮放和移動圖像,但我無法使用UIPanGestureRecognizer
移動圖像。我想使用UIScrollView
來縮放,但我想使用UIPanGestureRecognizer
移動UIImageView
。
我該怎麼做?
編輯
也許可以禁用或更改UIScrollView
的移動手勢識別器和轉發這些事件給UIImageView
?
我不想'shouldRecogniseSimultaneouslyWith ....'。我希望ScrollView在UIImageView上處理縮放和PanGestureRecognizer來處理移動。 – Mulgard