Swift - 添加 UIImageView 作为 UIWebView scrollView 和缩放的子视图 [英] Swift - Add UIImageView as subview of UIWebView scrollView and scaling

查看:21
本文介绍了Swift - 添加 UIImageView 作为 UIWebView scrollView 和缩放的子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIWebView 并且我已经成功地添加了一个 UIImage 视图到 UIWebViewscrollView 像所以:

I have a UIWebView and I have successfully added a UIImage view to the UIWebView’s scrollView like so:

let localUrl = String(format:"%@/%@", PDFFilePath, fileNameGroup)
        let url = NSURL.fileURLWithPath(localUrl)

        panRecognizer = UITapGestureRecognizer(target: self, action: #selector(panDetected))
        pinchRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(pinchDetected))

        panRecognizer.delegate = self
        pinchRecognizer.delegate = self

        webview = UIWebView()
        webview.frame = self.view.bounds
        webview.scrollView.frame = webview.frame

        webview.userInteractionEnabled = true
        webview.scalesPageToFit = true
        webview.becomeFirstResponder()
        webview.delegate = self
        webview.scrollView.delegate = self
        self.view.addSubview(webview)
        webview.loadRequest(NSURLRequest(URL:url))
        webview.gestureRecognizers = [pinchRecognizer, panRecognizer]

let stampView:StampAnnotation = StampAnnotation(imageIcon: UIImage(named: "approved.png"), location: CGPointMake(currentPoint.x, currentPoint.y))
            self.webview.scrollView.addSubview(stampView)

我的 UIWebView scrollView 是可扩展的.现在我正在寻找让 UIImageView(StampAnnotation 是一个类,UIImageView 是它的子类)缩放时/code> 秤.因此,如果用户放大 scrollViewUIImageView 会变大并保持在固定位置,如果用户缩小,UIImageView> 会变小,而 scrollView 在保持固定位置时会变小.

My UIWebView scrollView is scalable. Now I am looking for away to have my UIImageView (StampAnnotation is a class and UIImageView is its subclass) scale when the scrollView scales. So if the user zooms in on the scrollView, the UIImageView will get bigger and stay in a fixed position and if the user zooms out, the UIImageView will get smaller while the scrollView gets smaller while staying in a fixed position.

我真的希望这是有道理的.我尝试了以下方法:

I really hope that makes sense. I have tried the following:

func pinchDetected(recognizer:UIPinchGestureRecognizer)
    {

        for views in webview.scrollView.subviews
        {
            if(views.isKindOfClass(UIImageView))
            {
                views.transform = CGAffineTransformScale(views.transform, recognizer.scale, recognizer.scale)
                recognizer.scale = 1
            }
        }

        if(appDelegate.annotationSelected == 0)
        {
            webview.scalesPageToFit = true
        }
        else
        {
            webview.scalesPageToFit = false
        }

    }

但这没有任何作用,如果我删除这一行:

but this does nothing, if I remove this line:

recognizer.scale = 1

它的规模太大太快了.我的问题是,当 UIWebviewscrollView 滚动时,如何让我的 UIImageView 缩放?

it scales way too big too fast. My question is, how do I get my UIImageView to scale when the UIWebview’s scrollView scrolls?

任何帮助将不胜感激.

这解决了我的问题.

func scrollViewDidZoom(scrollView: UIScrollView) {

    for views in webview.scrollView.subviews
    {
        if(views.isKindOfClass(UIImageView))
        {
            views.transform = CGAffineTransformMakeScale(scrollView.zoomScale, scrollView.zoomScale)
        }
    }

}

不,它不会停留在页面上的固定位置,但我认为这是一个约束问题?

No it does not stay in a fixed position on the page, but I think that is a constraints issue?

推荐答案

你已经接近...

1) 添加一个 属性 以保留您的 stampViewFrame 的外部引用:

1) Add a property to hold onto an external reference for your stampViewFrame:

var stampViewFrame = CGRect(x: 100, y: 100, width: 100, height: 100)

2) 用这个替换你的 scrollViewDidZoom():

2) Replace your scrollViewDidZoom() with this:

 func scrollViewDidZoom(scrollView: UIScrollView) {
    for views in webView.scrollView.subviews
    {
        if(views.isKindOfClass(UIImageView))
        {
            views.frame = CGRect(x: stampViewFrame.origin.x * scrollView.zoomScale, y: stampViewFrame.origin.y * scrollView.zoomScale, width: stampViewFrame.width * scrollView.zoomScale, height: stampViewFrame.height * scrollView.zoomScale)
        }
    }
}

3) 最后,因为缩放比例在每个新的缩放操作开始时重置为 1,所以您需要调整您的 stampViewFrame 属性的值:

3) Finally, because the zoom scale resets to 1 at the begining of each new zooming action, you need to adjust the value of your stampViewFrame property:

func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
    stampViewFrame = CGRect(x: stampViewFrame.origin.x * scale, y: stampViewFrame.origin.y * scale, width: stampViewFrame.width * scale, height: stampViewFrame.height * scale)
}

我还尝试回答您关于方向更改期间布局的其他问题,但现在我对您要尝试执行的操作有了更好的了解.如果您希望您的 stampView 始终位于相对于 web 内容 的同一位置,您必须使用 HTML/JS,因为网页会动态地自行布局.一个更简单(希望足够接近)的解决方案是添加以下内容:

I also tried to answer your other question about layout during orientation change, but I now have a much better understanding of what you are trying to do. If you want your stampView to always be on in the same place relative to the web content, you have to get into HTML/JS because the webpage lays itself out dynamically. A much much more simple (and hopefully close enough) solution would be to add the following:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    webView.frame = view.bounds
    stampView.frame = stampViewFrame
}

这篇关于Swift - 添加 UIImageView 作为 UIWebView scrollView 和缩放的子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆