缩小时如何使 UIImageView 以 Scrollview 为中心? [英] How to make a UIImageView centered in a Scrollview when zoom out?

查看:61
本文介绍了缩小时如何使 UIImageView 以 Scrollview 为中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想找出一种方法来为 iPhone 制作一个普通的照片查看器,但我无法进行缩放.

Just trying to figure out a way to make a ordinary photo viewer for iPhone and I just couldn't make the zooming work.

    myimageview = UIImageView()
    myimageview.translatesAutoresizingMaskIntoConstraints = false
    myimageview.image = UIImage(named: "1")
    scrollview = UIScrollView()
    scrollview.delegate = self
    scrollview.backgroundColor = UIColor.brownColor()
    scrollview.minimumZoomScale = self.view.frame.width/myimageview.image!.size.width
    scrollview.maximumZoomScale = 1.0
    scrollview.translatesAutoresizingMaskIntoConstraints = false

    self.view.addSubview(scrollview)
    scrollview.addSubview(myimageview)
    let views = ["scrollview":scrollview,"myimageview":myimageview]
    //add scrollview constraints
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollview]|", options: [], metrics: nil, views: views))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollview]|", options: [], metrics: nil, views: views))
    scrollview.contentSize = myimageview.image!.size
    //add contentview:myimageview constraints
    let imagesize = myimageview.image!.size
    //let dd = (self.view.frame.height-imagesize.height*self.view.frame.width/imagesize.width)/2
    scrollview.addConstraint(NSLayoutConstraint(item: myimageview, attribute: .CenterY, relatedBy: .Equal, toItem: scrollview, attribute: .CenterY, multiplier: 1.0, constant: 0))
    scrollview.addConstraint(NSLayoutConstraint(item: myimageview, attribute: .Left, relatedBy: .Equal, toItem: scrollview, attribute: .Left, multiplier: 1.0, constant: 0))

    scrollview.addConstraint(NSLayoutConstraint(item: myimageview, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myimageview.image!.size.width))
    scrollview.addConstraint(NSLayoutConstraint(item: myimageview, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myimageview.image!.size.height))

所以基本上,我有一个滚动视图和一个图像视图,我将它添加到 uiscrollview 的子视图中.我手动设置滚动视图的内容大小并设置缩放.我还添加了 viewForZoomingInScrollView 委托函数.缩放效果很好.

So basically, I got a scrollview and I got an imageview and I added it to the subviews of uiscrollview. And I manually set the contentsize of the scrollview and I set the zooming. I also added the viewForZoomingInScrollView delegate function. Zooming works fine.

问题:缩小时,我希望图像显示在屏幕中间(uiscrollview),但现在它在顶部.正如你在下面看到的,图片在顶部,我希望它在屏幕的中间.看来 CenterY 不起作用.没有发现错误.我粘贴的代码在 ViewDidLoad 函数中.

Problem: when zoom out, I want the image to show in the middle of the screen(uiscrollview) but now it's on the top. As you can see below, the picture is on the top, I want it to be in the middle of the screen. It seems the CenterY doesn't work. No error has been found. The code I pasted is in the ViewDidLoad function.

推荐答案

好吧,我发现用了 2 年了,autolayout 还是解决不了这个问题,只能手动写了.好难过.

Ok, I found that still, after 2 yrs, autolayout can't solve this problem, you had to write it manually. Very sad.

var myimageview:UIImageView!
var scrollview:UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    myimageview = UIImageView()
    //myimageview.translatesAutoresizingMaskIntoConstraints = false
    myimageview.image = UIImage(named: "1")
    myimageview.frame = CGRectMake(0, 0, myimageview.image!.size.width, myimageview.image!.size.height)
    scrollview = UIScrollView()
    scrollview.delegate = self
    scrollview.backgroundColor = UIColor.brownColor()
    scrollview.minimumZoomScale = self.view.frame.width/myimageview.image!.size.width
    scrollview.maximumZoomScale = 1.0
    scrollview.translatesAutoresizingMaskIntoConstraints = false

    self.view.addSubview(scrollview)
    scrollview.addSubview(myimageview)
    let views = ["scrollview":scrollview,"myimageview":myimageview]
    //add scrollview constraints
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollview]|", options: [], metrics: nil, views: views))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollview]|", options: [], metrics: nil, views: views))
    scrollview.contentSize = myimageview.image!.size

}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    // center the image as it becomes smaller than the size of the screen
    return myimageview
}

func scrollViewDidZoom(scrollView: UIScrollView) {
    let boundsSize = self.scrollview.bounds.size
    var contentsFrame = self.myimageview.frame

    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
    } else {
        contentsFrame.origin.x = 0.0
    }

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
    } else {
        contentsFrame.origin.y = 0.0
    }

    myimageview.frame = contentsFrame;
}

这篇关于缩小时如何使 UIImageView 以 Scrollview 为中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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