【发布时间】:2018-02-24 00:41:34
【问题描述】:
我已将 nicklockwood 提供的 iCarousel 导入到 macOS 应用程序中。 https://github.com/nicklockwood/iCarousel
该应用程序是用 Swift 编写的。 在设法让一切正常工作(导入 .m 和 .h 文件,添加桥接头)之后,还有一件小事。
一旦应用程序启动并且 NSViewController 被激活,我会看到一个空白的 ViewController。只有当我开始调整视图大小时,我才会看到所有加载图片的 iCarousel。
我的代码如下所示:
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?
谢谢
【问题讨论】:
-
我已经手动添加了委托和数据源,因为我认为它可以解决我的问题。在我的初始版本中,我通过故事板和连接检查器连接了数据源和委托,结果相同。
标签: swift macos nsviewcontroller icarousel