使用滚动视图通过Swift放大图像 [英] Using a scroll view to zoom in an image with Swift

查看:157
本文介绍了使用滚动视图通过Swift放大图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Apple的"Swift应用开发"中学习Swift.

I'm learning Swift from Apple's "App Development in Swift".

我遇到了约束方面的问题,尤其是在第593页的"I Spy"实验室上.它基本上是希望您创建一个内部带有图像视图的滚动视图,并使它成为滚动视图,以便您可以放大图像并滚动就像打开照片时在照片"应用中一样.

I'm having problems with constraints, especially on the "I Spy" lab at page 593. It basically wants you to create a scroll view with an image view inside it and make it so you can zoom in an image and scroll it, like in the Photos app when you open a photo.

这本书说要使滚动视图和图像视图都与具有约束的屏幕一样大,因此我将top,bottom,前导和尾随约束添加到0,并且效果很好.

The book says to make both the scroll view and the image view as big as the screen with constraints, so I add top, bottom, leading and trailing constraints to 0, and it works fine.

当我添加图像视图时,我开始遇到问题.如果我设置与滚动视图相同的约束,则表示缺少X和Y位置的约束,并且不起作用. 如果我确实添加了这些约束,那么我将其水平和垂直对齐,则图像只会延伸到屏幕上,但仍然无法缩放或滚动.

When I add the Image View I start getting problems. If I set the same constraints as the scroll view, it says that it's missing constraints for X and Y positions, and it doesn't work. If I do add those constraints, so I make it aligned horizontally and vertically, the image just stretches to the screen, but I still can't zoom or scroll.

视图控制器的代码为:

class ViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.delegate = ViewController()
    updateZoomFor(size: view.bounds.size)
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imageView
}

func updateZoomFor(size: CGSize){
    let widthScale = size.width / imageView.bounds.width
    let heightScale = size.height / imageView.bounds.height
    let scale = min(widthScale,heightScale)
    scrollView.minimumZoomScale = scale
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


}

我尝试在Internet上四处寻找,但没有找到任何东西.我希望这不是一个愚蠢的问题,但我现在已经设法解决了这一天.

I tried looking around on the internet but didn't find anything. I hope this isn't a stupid question, but I tried to figured this out for a day now.

推荐答案

让我们从约束开始,您的滚动视图已正确添加, 现在,对于您的imageView,将其拖放到滚动视图中,以所需的方式指定左上右下角的约束,并将imageView垂直和水平居中放置,就像下面的屏幕截图一样

Let us begin with the constraints, your scrollview was added correctly, now for your imageView, drag and drop it inside your scrollview, specify the left right top and bottom constraints the way you want and center your imageView vertically and horizontally just like the screenshots below

让我们继续使用imageView.

Let us proceed with the imageView.

在情节提要上选择它,然后在助手编辑器中将内容模式更改为适合方面的方式:

Select it on your storyboard and in the assistant editor change the content mode to aspect fit like so :

现在让我们转到代码,

通过输入scrollView.delegate = ViewController(),您正在创建一个类型为ViewController的新实例,并且您没有使用现有实例,这就是为什么您应该将这一行替换为:

by typing scrollView.delegate = ViewController() you are creating a new instance of type ViewController and you're not using your existing one this is why you should replace this line with :

scrollView.delegate = self

这样,您将引用您的超类,即您现有的视图控制器

This way you will be referring to your superclass which is your existing view controller

并考虑添加此内容:

scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 5.0

scrollView.contentSize = .init(width: 2000, height: 2000)

最好, 马克

这篇关于使用滚动视图通过Swift放大图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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