ScrollView不会快速滚动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ScrollView不会快速滚动相关的知识,希望对你有一定的参考价值。

我有一种方法可以显示照片并在用户需要时放大。我使用滚动视图,以便用户放大照片时可以滚动。我的缩放工作但它放大到左上角,我的滚动视图不会在其他地方滚动,即使它显示负责滚动移动的垂直和水平条。

class ImageViewController: UIViewController, UIScrollViewDelegate {
var myscrollView: UIScrollView!

var imgView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
            myscrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
    self.view.addSubview(myscrollView)

    myscrollView.delegate = self
 //  myscrollView.scrollRectToVisible(CGRect(x: 100, y: 100, width: 1, height: 1), animated: true)

  //  myscrollView.setContentOffset(CGPoint(x: 0, y: view.frame.size.height), animated: true)
 //   myscrollView.contentSize = CGSize(width: view.frame.width, height: view.frame.height)
    print("(myscrollView.frame.height)  (myscrollView.frame.width)")
    //myscrollView.contentSize = CGSize(width: 1000, height: 2000)
    myscrollView.isScrollEnabled = true
    myscrollView.minimumZoomScale = 1.0
    myscrollView.maximumZoomScale = 10.0//maximum zoom scale you want
    myscrollView.zoomScale = 1.0
    myscrollView.alwaysBounceVertical = false
    myscrollView.alwaysBounceHorizontal = false
    myscrollView.showsVerticalScrollIndicator = true
    myscrollView.flashScrollIndicators()

  //  scrollView.isPagingEnabled = false
  //  self.scrollView.contentSize = self.view.frame.size * 2//or what ever size you want to set
  //  myscrollView.contentOffset = CGPointMake((myscrollView.contentSize.width-myscrollView.frame.size.width)*.5f, .0f);//scroll to center
   // myscrollView.contentOffset = CGPoint((myscrollView.contentSize.width-myscrollView.frame.size.width)*.5f, .0f)

    myscrollView.addSubview(imgView)
    //self.view.addSubview(imgView)
    imgView!.layer.cornerRadius = 11.0
    imgView.bounds = myscrollView.bounds
    //imgView!.clipsToBounds = false
    imgView.isUserInteractionEnabled = true
    imgView.heightAnchor.constraint(equalToConstant: 50).isActive = true
   // imgView.widthAnchor.constraint(equalToConstant: 50).isActive = true
    imgView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 64).isActive = true
    imgView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
    imgView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
   // imgView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
    imgView.translatesAutoresizingMaskIntoConstraints = false
    myscrollView.contentSize = CGSize(width: imgView.frame.width , height: 2000)
   // myscrollView.contentSize = imgView.bounds.size

    // Do any additional setup after loading the view.
}
  //    func scrollViewDidZoom(_ scrollView: UIScrollView) {
  //        var offsetY: CGFloat!
 //        offsetY = 0;
 //        if (scrollView.zoomScale > 1){
 //            offsetY = CGFloat(scrollView.contentOffset.y);
 //        }
//        //CGPoint(x: scrollView.contentOffset.x, y: offsetY)
//        scrollView.setContentOffset(CGPoint(x: 
 // scrollView.contentOffset.x, y: offsetY), animated: true)
  //
  //       // [aScrollView setContentOffset: 
   // CGPointMake(aScrollView.contentOffset.x, offsetY)];
   //
 //    }

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imgView
}
//    override func viewDidLayoutSubviews() {
//        myscrollView.delegate = self
//        myscrollView.contentSize = 
//CGSize(width:self.view.frame.size.width, height: 1000)
//    }
//    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
 //        return imgView
 //    }
  }

我试图手动将contentSize设置为一些大或小的值,以及使用frame.width和height,但它们似乎都没有工作。有人可以请帮忙解决我的问题吗?提前致谢 :)

答案

试试这些代码并查看,你有什么遗漏 -

let screenSize: CGRect = UIScreen.main.bounds

let scrollView = UIScrollView()

override func viewDidLoad() {
        super.viewDidLoad()



        scrollView.contentSize = CGSize(width: screenSize.width, height: 1300)
        scrollView.frame = CGRect(x: 0, y: 70, width: screenSize.width, height: screenSize.height-70)
        scrollView.backgroundColor = UIColor.clear

        view.addSubview(scrollView)


}
另一答案

尝试实施scrollViewDidZoom方法如下;

func scrollViewDidZoom(_ scrollView: UIScrollView) {

        let offsetX = max((scrollView.bounds.size.width-scrollView.contentSize.width) * 0.5, 0.0)
        let offsetY = max((scrollView.bounds.size.height-scrollView.contentSize.height) * 0.5, 0.0)
        self.scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0)
    }

以上是关于ScrollView不会快速滚动的主要内容,如果未能解决你的问题,请参考以下文章

我应该使用 ScrollView 还是 RecyclerView 在片段中滚动?

带有 TabLayout 的片段内的 ScrollView 不滚动,为啥?

当scrollview快速滚动时如何滚动collectionView?

向上推键盘 Swift iOS 后,我的 Scrollview 不会一直向上滚动

ViewPager里面的ScrollView,自动滚动到中间

scrollView 不能快速滚动