【问题标题】:adding NSImageView to NSScrollView将 NSImageView 添加到 NSScrollView
【发布时间】:2015-11-13 23:20:53
【问题描述】:

我有一张尺寸约为 200x2000 像素的图像。我想显示以 200x200 矩形为中心的图像,但我希望能够上下移动它。最好我能想出我需要将一个 NSImageView 添加到一个 NSScrollView 但我不知道如何或即使这是最好的方法。这是我开发 OS X 的第一天...

经过一番谷歌搜索后,我找到了this,从中我可以想出这个

class MasterViewController: NSViewController {

var Photo: NSImageView!
@IBOutlet var scroll: NSScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    var imageRect: NSRect
    self.Photo = NSImageView.init()
    self.Photo.image = NSImage.init(named:"horizon")

    imageRect = NSMakeRect(0.0, 0.0, self.Photo.image!.size.width, self.Photo.image!.size.height)
    print("image size", imageRect)
    self.Photo = NSImageView(frame: imageRect)
    self.Photo.setBoundsSize(NSSize(width: imageRect.width, height: imageRect.height))
    self.Photo.imageScaling = NSImageScaling.ScaleNone


    self.scroll.setFrameSize(NSSize(width: imageRect.width,height: imageRect.width))
    self.scroll.hasVerticalScroller = true
    self.scroll.hasHorizontalScroller = true
    self.Photo.setFrameSize(CGSize(width: imageRect.width,height: imageRect.width))
    self.scroll.documentView = self.Photo
    //print(self.scroll.documentView?.frame)

    //self.scroll.setC contentSize = NSSize(width: 200, height: 2000)
    //self.Photo.image = NSImage.init(named:"bezel")
    //self.scroll.addSubview(self.Photo)

}

但我无法让图像显示在滚动视图中

【问题讨论】:

    标签: swift macos nsscrollview nsimageview


    【解决方案1】:

    @lusher00:在您的示例中,您初始化了两次 self.Photo,首先使用 init(),然后使用 (frame: imageRect),这可能解释了为什么图像不显示。

    你可以将imageView设置为NSScrollView的documentView,如下:

            @IBOutlet var scrollView: NSScrollView!
    
    
            var imageRect: NSRect
            // Initialize below the imageView image with appropriate content (to adapt)
            imageView.image = NSImage.init(named:"horizon")
            imageRect = NSMakeRect(0.0, 0.0, imageView.image!.size.width, imageView.image!.size.height)
            imageView.setFrameSize(CGSize(width: imageRect.width, height: imageRect.height))
            imageView.imageScaling = NSImageScaling.ScaleNone
            scrollView.documentView = imageView
            scrollView.hasVerticalScroller = true
            scrollView.hasHorizontalScroller = true
    

    【讨论】:

      【解决方案2】:

      只需将图像视图添加为滚动视图的子视图。

      scrollView.addSubview(imageView)
      

      你可以设置imageview的位置,将它定位在scrollview里面。

      滚动视图需要知道其内容大小有多大才能启用该区域的滚动。所以不要忘记更新滚动视图的contentSize 属性。

      例如将 200x200 的 imageView 添加到具有 200x200 框架的 scrollView 中。将 contentSize 设置为 400x400 并调用 imageView.center = scrollView.center,将使图像居中并允许在滚动视图的 200x200 可见框架内围绕图像进行一些滚动。

      你也可以通过查看contentOffset来获取scrollView的当前偏移量。

      如果您需要在用户滚动时进行跟踪,可以使用scrollViewDidScroll。检查 docs 的滚动视图以获取其他一些选项。

      【讨论】:

      • 到目前为止,除了设置 contentSize 之外,我基本上已经完成了所有这些工作。但是, contentSize 是一个只能获取的属性。我仍然有一个无法滚动的居中图像
      • 有一个documentView,对不起,我是从iOS术语解释的。检查这个stackoverflow.com/questions/3457926/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多