简体   繁体   English

ScrollView不会在swift中滚动

[英]ScrollView doesn't scroll in swift

I have an UIView and add an UIScrollView to it. 我有一个UIView并添加一个UIScrollView。 Inside the scrollView is an UIImageView. scrollView内部是一个UIImageView。
I want to zoom the image view by pressing a button. 我想通过按下按钮来缩放图像视图。 If the image view is zoomed you should be able to scroll but that's not working. 如果图像视图被缩放,您应该能够滚动,但这不起作用。

Currently I have that: 目前我有:

self.imageView = UIImageView()
self.imageView.image = image
self.imageView.frame = self.contentView.bounds

self.scrollView = UIScrollView()
self.scrollView.frame = self.contentView.bounds
self.scrollView.contentSize = self.contentView.bounds.size
self.scrollView.addSubview(self.imageView)

self.contentView.addSubview(self.scrollView)

and that: 然后:

@IBAction func zoomPicture() {
    if (scale <= 1.5) {
        scale = scale + 0.1
        scrollView.transform = CGAffineTransformMakeScale(scale, scale)
        scrollView.zoomScale = scale
        scrollView.maximumZoomScale = 1.5
        scrollView.minimumZoomScale = 1.0
        scrollView.contentSize = contentView.bounds.size
        scrollView.scrollEnabled = true
    }
}

my class also implements UIScrollViewDelegate and I set the delegate in the viewDidLoad() function. 我的类也实现了UIScrollViewDelegate ,我在viewDidLoad()函数中设置了委托。 I have also added the viewForZoomingInScrollView function. 我还添加了viewForZoomingInScrollView函数。 The zoom works but I can't scroll. 缩放工作,但我无法滚动。

For this issue you have to set self.scrollView.contentSize bigger than self.contentView.bounds so it get proper space to scroll. 对于此问题,您必须将self.scrollView.contentSize设置self.scrollView.contentSize大于self.contentView.bounds以便获得适当的滚动空间。

replace this line 替换这一行

self.scrollView.contentSize = self.contentView.bounds.size

to this: 对此:

self.scrollView.contentSize = self.contentView.bounds.size*2 //or what ever size you want to set

Try this: 试试这个:

@IBAction func zoomPicture() {
    if (scale <= 1.5) {
        scale = scale + 0.1
        scrollView.zoomScale = scale
        scrollView.maximumZoomScale = 1.5
        scrollView.minimumZoomScale = 1.0
        let size = contentView.bounds.size
        scrollView.contentSize = CGSize(width: size.width * scale, height: size.height * scale)
        scrollView.scrollEnabled = true
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM