我在UIViewController中有一个UIScrollView,并使用UITapGestureRecognizer动态地将“rectView”(UIView)添加到contentView(UIView添加到UIScrollView),并且可以通过UIPanGestureRecognizer移动(拖动,平移)矩形。
它工作正常,但在我将scrollview缩放到大(例如120%)之后,我再也无法拖动rect而不是滚动scrollview,我不想在拖动rect时滚动滚动视图任何想法?
抱歉我的英语不好,希望你能理解我在说什么,如果需要我可以上传xcode项目,谢谢
import UIKit class StageVC: UIViewController,UIScrollViewDelegate,UIGestureRecognizerDelegate { @IBOutlet var scrollView: UIScrollView! private var contentView:UIView! override func viewDidLoad() { super.viewDidLoad() self.scrollView.minimumZoomScale = 0.025 self.scrollView.maximumZoomScale = 2 self.scrollView.delegate = self self.contentView = UIView(frame: CGRect(x: 0, y: 0, width:self.view.frame.width, height: 10000)) self.contentView.backgroundColor = UIColor.red self.scrollView.addSubview(self.contentView) let cs = CGSize(width: self.contentView.frame.width, height: self.contentView.frame.height) self.scrollView.contentSize = cs let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction)) self.contentView.addGestureRecognizer(gesture) gesture.delegate = self } @objc func checkAction(sender : UITapGestureRecognizer) { let position = sender.location(in: self.contentView) let rectView = UIView(frame: CGRect(x: position.x - 50, y: position.y - 50, width: 100, height: 100)) rectView.backgroundColor = UIColor.green self.contentView.addSubview(rectView) let pan = UIPanGestureRecognizer(target: self, action: #selector(StageVC.panHandler(sender:))) rectView.addGestureRecognizer(pan) } @objc func panHandler(sender:UIPanGestureRecognizer){ let panView = sender.view! let t = sender.translation(in: self.contentView) if (sender.state == .began || sender.state == .changed){ panView.center = CGPoint(x:panView.center.x + t.x,y:panView.center.y + t.y) sender.setTranslation(CGPoint.zero, in: contentView) } else if(sender.state == .ended) { } } func viewForZooming(in scrollView: UIScrollView) -> UIView? { return self.contentView } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }