0
我已将nicklockwood提供的iCarousel导入到macOs应用程序中。 https://github.com/nicklockwood/iCarouseliCarousel仅在调整大小后才显示图像
该应用程序是用Swift编写的。 在设法使所有工作都正常(导入.m和.h文件,添加Bridging Header)之后,有一件小事。
一旦应用程序启动,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?
谢谢
我已经手动添加委托和数据源,因为我认为它可以解决我的问题。在我的初始版本中,我通过故事板和连接检查器连接了数据源和委托,结果相同。 –