gpt4 book ai didi

swift - iCarousel 仅在调整大小后显示图像

转载 作者:行者123 更新时间:2023-11-28 15:19:43 24 4
gpt4 key购买 nike

我已经将 nicklockwood 提供的 iCarousel 导入到 macOs 应用程序中。 https://github.com/nicklockwood/iCarousel

该应用程序是用 Swift 编写的。在设法使一切正常工作后(导入 .m 和 .h 文件,添加桥接 header ),还有一件小事。

应用程序启动并激活 NSViewController 后,我会看到一个空白的 ViewController。只有当我开始调整 View 大小时,我才会看到带有所有已加载图片的 iCarousel。

View after app start

View after resizing

我的代码如下所示:

class CarouselViewController: NSViewController, iCarouselDataSource, iCarouselDelegate {

var images = [NSImage]()

@IBOutlet var carousel: iCarousel!

override func awakeFromNib() {
super.awakeFromNib()
loadImages()

}

override func viewDidLoad() {
super.viewDidLoad()

self.carousel.type = iCarouselType.coverFlow
self.carousel.dataSource = self
self.carousel.delegate = self

carousel.reloadData()
}

func loadImages() {
let filePath = "/pics/"
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.picturesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
let path = paths?.appending(filePath)

//
let fileManager = FileManager.default
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: path!)!
while let element = enumerator.nextObject() as? String {
if element.hasSuffix("jpg") || element.hasSuffix("png") {
if let image = NSImage(contentsOfFile: path! + element) {
print("File: \(path!)\(element)")
self.images.append(image)
}
}
}
}

func numberOfItems(in carousel: iCarousel) -> Int {
return images.count
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: NSView?) -> NSView {
let imageView = NSImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
imageView.image = self.images[index]
imageView.imageScaling = .scaleAxesIndependently
return imageView
}

func carouselItemWidth(_ carousel: iCarousel) -> CGFloat {
return 200.0
}

}

如何在不先调整大小的情况下显示我的 iCarousel?

谢谢

最佳答案

我想我找到了一个解决方案:

轮播中实际图像的绘制是由 layOutItemViews 或 layoutSubviews 触发的。一个可公开访问的函数是 setType,它允许设置轮播类型。如果我在分配数据源和加载数据后设置类型,图像将正常显示。所以最简单的答案是像下面这样更改 viewDidLayout:

override func viewDidLoad() {
super.viewDidLoad()
self.carousel.dataSource = self
self.carousel.delegate = self

carousel.reloadData()

// Triggers layOutItemView and thus will render all images.
self.carousel.type = iCarouselType.coverFlow

}

现在也可以通过 Storyboard和连接检查器分配数据源。

关于swift - iCarousel 仅在调整大小后显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46229115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com