Swift - 在旋转时将UIImageView保持在相同位置

use*_*331 9 ios swift

我有UIWebView,在其scrollView中,我添加了一个UIImageView,如下所示:

self.webview.scrollView.addSubview(image)
Run Code Online (Sandbox Code Playgroud)

我的问题是当我将设备从纵向旋转到横向时,UIImageView没有停留在我最初在scrollView上设置的位置,我理解屏幕的宽度和高度变化,我想我想要改变它在我的UIImageView上的位置,所以它似乎没有改变.

我有这个方法:

 override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

        var newScrollViewFrame = webview.scrollView.frame
        var newFrame = webview.frame

        newFrame.size.width = size.width
        newFrame.size.height = size.height

        newScrollViewFrame.size.width = size.width
        newScrollViewFrame.size.height = size.height

        webview.frame = newFrame
        webview.scrollView.frame = newScrollViewFrame

    }
Run Code Online (Sandbox Code Playgroud)

此方法中的当前代码只调整UIWebView及其滚动视图的大小,但不调整scrollView中的UIImageViews

任何帮助,将不胜感激.

谢谢,

我尝试过以下方法:

for views in webview.scrollView.subviews
        {
            if(views.isKindOfClass(UIImageView))
            {
                views.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)/2);

            }
        }
Run Code Online (Sandbox Code Playgroud)

但这会在旋转时侧向放置UIImageView

以下是我如何将webview添加到视图中:

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)
Run Code Online (Sandbox Code Playgroud)

我有一半解决了我的问题,但做了以下事情:

webview.translatesAutoresizingMaskIntoConstraints = false

let horizontalConstraint = NSLayoutConstraint(item: webview, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        view.addConstraint(horizontalConstraint)

        let verticalConstraint = NSLayoutConstraint(item: webview, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
        view.addConstraint(verticalConstraint)

        let widthConstraint = NSLayoutConstraint(item: webview, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0.1, constant: 500)
        view.addConstraint(widthConstraint)

        let heightConstraint = NSLayoutConstraint(item: webview, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 500)
        view.addConstraint(heightConstraint)
Run Code Online (Sandbox Code Playgroud)

但是现在,UIWebView不是全宽或高度:( Sooooo关闭.

我也试过这个,但是UIImageView不会保留在同一个地方.

let left = NSLayoutConstraint(item: self.webView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 0) 

let right = NSLayoutConstraint(item: self.webView, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant: 0) 

let top = NSLayoutConstraint(item: self.webView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0) 

let bottom = NSLayoutConstraint(item: self.webView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: 0) 

NSLayoutConstraint.activateConstraints([top, bottom, left, right])
Run Code Online (Sandbox Code Playgroud)

我也尝试过向UIImageView添加约束

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

            let left = NSLayoutConstraint(item: stampView, attribute: .Left, relatedBy: .Equal, toItem: self.webview.scrollView, attribute: .Left, multiplier: 1, constant: 0)

            let right = NSLayoutConstraint(item: stampView, attribute: .Right, relatedBy: .Equal, toItem: self.webview.scrollView, attribute: .Right, multiplier: 1, constant: 0)

            let top = NSLayoutConstraint(item: stampView, attribute: .Top, relatedBy: .Equal, toItem: self.webview.scrollView, attribute: .Top, multiplier: 1, constant: 0)

            let bottom = NSLayoutConstraint(item: stampView, attribute: .Bottom, relatedBy: .Equal, toItem: self.webview.scrollView, attribute: .Bottom, multiplier: 1, constant: 0)

            NSLayoutConstraint.activateConstraints([top, bottom, left, right])
Run Code Online (Sandbox Code Playgroud)

同样的结果,UIImageView不会停留在同一个地方.

UPDATE

我的网页浏览:

webview = UIWebView()
        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]
        webview.translatesAutoresizingMaskIntoConstraints = false

        let left = NSLayoutConstraint(item: webview, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 0)

        let right = NSLayoutConstraint(item: webview, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant: 0)

        let top = NSLayoutConstraint(item: webview, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0)

        let bottom = NSLayoutConstraint(item: webview, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: 0)

        NSLayoutConstraint.activateConstraints([top, bottom, left, right])
Run Code Online (Sandbox Code Playgroud)

的UIImageView

stampView.translatesAutoresizingMaskIntoConstraints = false

            let left = NSLayoutConstraint(item: stampView, attribute: .Left, relatedBy: .Equal, toItem: self.webview.scrollView, attribute: .Left, multiplier: 1, constant: 100)

            let right = NSLayoutConstraint(item: stampView, attribute: .Right, relatedBy: .Equal, toItem: self.webview.scrollView, attribute: .Right, multiplier: 1, constant: 0)

            let top = NSLayoutConstraint(item: stampView, attribute: .Top, relatedBy: .Equal, toItem: self.webview.scrollView, attribute: .Top, multiplier: 1, constant: 100)

            let bottom = NSLayoutConstraint(item: stampView, attribute: .Bottom, relatedBy: .Equal, toItem: self.webview.scrollView, attribute: .Bottom, multiplier: 1, constant: 0)

            let width = NSLayoutConstraint(item: stampView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0.1, constant: 150)

            let height = NSLayoutConstraint(item: stampView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 73)

            NSLayoutConstraint.activateConstraints([left, right, top, bottom, width, height])
Run Code Online (Sandbox Code Playgroud)

我只需要弄清楚数学让这个设备处于触摸位置.(百分比?)

Car*_*han 2

我认为这些事情在不使用自动布局的情况下总是更容易完成。为此,我建议使用 viewDidLayoutSubviews()。这是我的代码:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    webView.frame = view.bounds

    let screen = UIScreen.mainScreen().fixedCoordinateSpace

    //These values will give a rect half the size of the screen and centered.
    let width = screen.bounds.width / 2
    let height = screen.bounds.height / 2
    let x = (screen.bounds.width - width) / 2
    let y = (screen.bounds.height - height) / 2
    let absoluteRect = CGRect(x: x, y: y, width: width, height: height)

    let stampRect = screen.convertRect(absoluteRect, toCoordinateSpace: webView)
    stampView.frame = stampRect

    //Change the orientation of the image
    switch UIDevice.currentDevice().orientation {
    case .LandscapeLeft:
        stampView.image = UIImage(CGImage:originalImage.CGImage!, scale:originalImage.scale, orientation: UIImageOrientation.Left)
        break
    case .LandscapeRight:
        stampView.image = UIImage(CGImage:originalImage.CGImage!, scale:originalImage.scale, orientation: UIImageOrientation.Right)
        break
    default:
        stampView.image = UIImage(CGImage:originalImage.CGImage!, scale:originalImage.scale, orientation: UIImageOrientation.Up)
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里做了几件事...首先我设置了 webViewFrame。接下来,定义相对于屏幕的绝对坐标系会很有帮助。当手机方向改变时,屏幕的值不会改变(允许您将 imageView 保持在同一位置。)接下来,我为 stampView 定义所需的框架,然后将绝对框架转换为滚动视图中的等效框架(它是 superView) )并将其分配给 stampView。最后,如果您希望 stampView 中的图像始终方向正确,则需要更改图像方向。CGAffineTransformMakeRotation 仅在您的视图为正方形时才有效,但我们可以通过实际更改 stampView 中的 imageOrientation 使其更通用。这需要原始图像的属性:

let originalImage = UIImage(named: "image_name")!
Run Code Online (Sandbox Code Playgroud)

最后,由于横向到横向的转换不会调用 viewWillLayoutSubViews(),因此请执行以下操作:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    view.setNeedsLayout()
}
Run Code Online (Sandbox Code Playgroud)

此代码并不能实现最漂亮的转换,但它应该有助于解决您的布局问题。