双击可放大/缩小

时间:2017-05-26 18:10:28

标签: ios swift zoom pinchzoom

当用户在文本列表视图屏幕中按下按钮时,我有一个显示一堆图片的应用程序。我有一个特定的子类,允许用户捏和放大,但我很好奇我需要在我的特定swift文件中输入什么代码,以允许用户双击放大和缩小。我使用此代码得到三个错误。两个说“类型'ZoomingScrollView'的值没有成员'scrollview'或'imageview'”而且“'CGRectZero'在Swift中是不可用的”我允许用户放大的子类代码与代码I一起下面我正在使用双击来缩放。

import UIKit

class ZoomingScrollView: UIScrollView, UIScrollViewDelegate {

 @IBOutlet weak var viewForZooming: UIView? = nil {
didSet {
    self.delegate = self
} }

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

@IBAction func userDoubleTappedScrollview(recognizer:  UITapGestureRecognizer) {
if let scrollV = self.scrollview {
    if (scrollV.zoomScale > scrollV.minimumZoomScale) {
        scrollV.setZoomScale(scrollV.minimumZoomScale, animated: true)
    }
    else {
        //(I divide by 3.0 since I don't wan't to zoom to the max upon the double tap)
        let zoomRect = self.zoomRectForScale(scrollV.maximumZoomScale / 3.0, center: recognizer.locationInView(recognizer.view))
        self.scrollview?.zoomToRect(zoomRect, animated: true)
    }
} }

func zoomRectForScale(scale : CGFloat, center : CGPoint) -> CGRect {
var zoomRect = CGRectZero
if let imageV = self.imageView {
    zoomRect.size.height = imageV.frame.size.height / scale;
    zoomRect.size.width  = imageV.frame.size.width  / scale;
    let newCenter = imageV.convertPoint(center, fromView: self.scrollview)
    zoomRect.origin.x = newCenter.x - ((zoomRect.size.width / 2.0));
    zoomRect.origin.y = newCenter.y - ((zoomRect.size.height / 2.0));
}
return zoomRect; }}

1 个答案:

答案 0 :(得分:4)

问题似乎是您从具有scrollView和imageView插座的视图控制器复制了一些代码。我猜你的viewForZooming:UIView是你正在放大的图像视图。你也不必再引用self.scrollView了,因为self是滚动视图,因为你是子类滚动视图:)我认为下面的代码应该解决你的问题(注意:它是最新的swift语法。你发布的是什么不,所以你可能必须在必要时将代码切换回旧样式。你应该尽量使用最新的Swift)。祝你好运,如果你有疑问,请告诉我。

import UIKit

class ZoomingScrollView: UIScrollView, UIScrollViewDelegate {

    @IBOutlet weak var viewForZooming: UIView? = nil {
        didSet {
            self.delegate = self
        } }

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

    @IBAction func userDoubleTappedScrollview(recognizer:  UITapGestureRecognizer) {
        if (zoomScale > minimumZoomScale) {
            setZoomScale(minimumZoomScale, animated: true)
        }
        else {
            //(I divide by 3.0 since I don't wan't to zoom to the max upon the double tap)
            let zoomRect = zoomRectForScale(scale: maximumZoomScale / 3.0, center: recognizer.location(in: recognizer.view))
            zoom(to: zoomRect, animated: true)
        }
    }

    func zoomRectForScale(scale : CGFloat, center : CGPoint) -> CGRect {
        var zoomRect = CGRect.zero
        if let imageV = self.viewForZooming {
            zoomRect.size.height = imageV.frame.size.height / scale;
            zoomRect.size.width  = imageV.frame.size.width  / scale;
            let newCenter = imageV.convert(center, from: self)
            zoomRect.origin.x = newCenter.x - ((zoomRect.size.width / 2.0));
            zoomRect.origin.y = newCenter.y - ((zoomRect.size.height / 2.0));
        }
        return zoomRect;
    }
}