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

我有一个UIWebView ,我已经成功地向UIWebViewscrollView添加了一个UIImage视图, 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 scrollView是可扩展的。 现在我正在寻找让我的UIImageViewStampAnnotation是一个类, UIImageView是它的子类)的尺度,当scrollView缩放时。 因此,如果用户放大scrollViewUIImageView将变大并保持在固定位置,如果用户缩小, 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 

它太大了太大了。 我的问题是,当UIWebviewscrollView滚动时,如何让我的UIImageView扩展?

任何帮助,将不胜感激。

这解决了我的问题。

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

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

你很亲密……

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 } 

使用scrollViewDidZoom的scroll delegate方法:

  func scrollViewDidZoom(scrollView: UIScrollView){ //Change the subview of scroll frame as per the scroll frame scale //rect = initial position & size of the image. stampView.frame = CGRectMake((CGRectGetMaxX(rect)-rect.size.width)*webView.scrollView.zoomScale, (CGRectGetMaxY(rect)-rect.size.height)*webView.scrollView.zoomScale, rect.width*webView.scrollView.zoomScale,rect.height*webView.scrollView.zoomScale) }