ScrollView不会在swift中滚动

时间:2016-08-09 06:03:38

标签: ios swift uiscrollview uiimageview

我有一个UIView并添加一个UIScrollView。 scrollView内部是一个UIImageView 我想通过按下按钮来缩放图像视图。如果图像视图被缩放,您应该能够滚动,但这不起作用。

目前我有:

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)

那:

@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
    }
}

我的类也实现了UIScrollViewDelegate,并在viewDidLoad()函数中设置了委托。我还添加了viewForZoomingInScrollView函数。缩放工作,但我无法滚动。

2 个答案:

答案 0 :(得分:5)

对于此问题,您必须将self.scrollView.contentSize设置为大于self.contentView.bounds,以便获得适当的滚动空间。

替换此行

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

到此:

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

答案 1 :(得分:0)

试试这个:

@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
    }
}