ScrollView,缩放功能无法正常工作

时间:2015-02-11 11:40:22

标签: ios swift uiscrollview

我正在使用swift创建一个ios应用程序。在我的一个ViewController中,我有几个UIView,每个都负责只显示图片的一部分(所以我使用ScrollView,对吧?)。我有子类UIView来做那个

import UIKit

    class LittlePicture: UIView, UIScrollViewDelegate {

    var scrollView = UIScrollView()
    var imageView = UIImageView()
    var image:UIImage!

    init(frame: CGRect, image:UIImage, x:CGFloat, y:CGFloat, zoom: CGFloat) {
        super.init(frame: frame)

        scrollView.frame = self.frame
        scrollView.delegate = self
        scrollView.userInteractionEnabled = false
        self.addSubview(scrollView)

        setPhoto(image, x: x, y: y, zoom: zoom)

        scrollView.addSubview(imageView)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func setPhoto(image:UIImage,x:CGFloat,y:CGFloat,zoom:CGFloat){

        self.image = image

        scrollView.contentSize = image.size

        imageView.image = image
        imageView.frame = CGRect(origin: CGPointZero, size: image.size)

        scrollView.minimumZoomScale = 0.001
        scrollView.maximumZoomScale = 100.0
        scrollView.zoomScale = zoom
        scrollView.contentOffset = CGPoint(x: x, y: y)

    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }

}

由于要展示的图片或部分图片可以更改,我已实现了一个函数setPhoto。问题是,当我调用此函数时,ScrollView不会缩放任何内容(scrollView.contentSize在调用scrollView.zoomScale = zoom之前和之后保持不变)

在我的UIViewController中(它仅用于测试......):

import UIKit

class ViewController: UIViewController {

    var l:LittlePicture!

    @IBAction func changePicture(sender:AnyObject){
        l.setPhoto(UIImage(named: "a.jpg")!, x: 0, y: 0, zoom: 0.05)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        l=LittlePicture(frame: CGRect(x: 0, y: 0, width: 300, height: 300), image: UIImage(named: "a.jpg")!, x: 0, y: 0, zoom: 0.05)
        view.addSubview(l)
    }


}

当我点击按钮时,缩放从0.05到1.0。事实上,如果我记录scrollView.zoomScale,我获得0.05,但我在屏幕上看到的是一个等于1.0的缩放

有什么想法吗?我是iOS开发的新手..

0 个答案:

没有答案