Swift - 添加UIImageView作为UIWebView scrollView和缩放的子视图

时间:2016-07-23 01:36:29

标签: ios swift uiwebview

我有一个UIWebView,我已成功将UIImage视图添加到UIWebView的{​​{1}},如下所示:

scrollView

我的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具有可扩展性。现在,我正在寻找我的scrollViewUIImageView是一个类,而StampAnnotation是它的子类),当UIImageView缩放时。因此,如果用户放大scrollViewscrollView会变大并保持固定位置,如果用户缩小,UIImageView会变小UIImageView在保持固定位置时变小。

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

scrollView

但如果删除此行,则无效:

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
        }

    }

它太大了太大了。我的问题是,当recognizer.scale = 1 的{​​{1}}滚动时,如何让UIImageView缩放?

任何帮助都将不胜感激。

这解决了我的问题。

UIWebview

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

2 个答案:

答案 0 :(得分:1)

使用MenuItem的滚动委托方法:

Intent intent=new Intent(v.getContext(),FinalResultActivity.class);
        intent.putExtra("questions_count", questions_count);
        intent.putExtra("questions_correct", questions_correct);
        intent.putExtra("questions_score", questions_score);
        intent.putExtra("questions_correct_list", questions_correct_list);

        v.getContext().startActivity(intent);

答案 1 :(得分:1)

你很亲密......

1)添加属性以保留stampViewFrame的外部参考:

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

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

 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属性的值:

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,因为网页会自动生成。一个更简单(并且希望足够接近)的解决方案是添加以下内容:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

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