swift - ScrollView 滚动速度不快

标签 swift uiscrollview zooming

我有一种方法可以显示照片并在用户需要时放大它。我使用了 ScrollView ,以便用户在放大照片时可以滚动。我的缩放工作正常,但它放大到左上角,并且我的 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)


}

关于swift - ScrollView 滚动速度不快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49106274/

相关文章:

ios - 如何根据表格行设置动态表格高度?

java - 正确调整 JScrollPane 及其内容的大小

iphone - 在 iphone 应用程序中捏合以放大 UIWebView

Swift 应用程序元素未及时从 StoryBoard 加载

ios - 具有不同大小单元格的 UICollectionView

ios - 应用程序因生物识别而关闭时崩溃

ios - 在 obj c 中以编程方式为 subview 添加约束(容器边距的前导和尾随空间)

objective-c - 使 UIScrollView 的 subview 固定,而其他 subview 可滚动

android - 使用缩放和滚动打开 ImageView

ios - 是否可以在 UITableViewCell 的单个按钮上快速设置文本