ScrollView无法快速滚动

时间:2018-03-05 08:32:59

标签: swift uiscrollview zoom

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

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,但它们似乎都没有工作。有人可以请帮忙解决我的问题吗?在此先感谢:)

2 个答案:

答案 0 :(得分:0)

试试这些代码并查看“你错过了什么 -

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)


}

答案 1 :(得分:0)

尝试按以下方式实施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)
    }