ShelfView (iOS)
用于在书架上显示书籍的 iOS 自定义视图
(此处可找到 Android 版本)
要求
- iOS 10.0+
- Swift 4.2
安装
ShelfView
通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile 中
pod 'ShelfView'
注意
由于 book covers 的 URL 为 http
,请按以下方式更新您的 info.plist
:
- 将
App Transport Security Settings
添加到列表中 - 将
Allow Arbitrary Loads
添加到上面添加的安全设置中;将其设置为YES
。
普通架子
import ShelfView
class PlainShelfController: UIViewController, PlainShelfViewDelegate {
var shelfView: PlainShelfView!
override func viewDidLoad() {
super.viewDidLoad()
let books = [
BookModel(bookCoverSource: "https://files.kerching.raywenderlich.com/covers/d5693015-46b6-44f8-bf7b-7a222b28d9fe.png", bookId: "0", bookTitle: "Realm: Building Modern Swift Apps with Realm"),
BookModel(bookCoverSource: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTYEkCQ_wu8HoGJzzs_gUH_FVusgI2RhntBKQ-WkmqnDJZnriwY6Q", bookId: "1", bookTitle: "iOS 10 by Tutorials: Learning the new iOS APIs with Swift 3")
]
shelfView = PlainShelfView(frame: CGRect(x: 0, y: 0, width: 350, height: 500), bookModel: books, bookSource: PlainShelfView.BOOK_SOURCE_URL)
shelfView.delegate = self
self.view.addSubview(shelfView)
}
func onBookClicked(_ shelfView: PlainShelfView, index: Int, bookId: String, bookTitle: String) {
print("I just clicked \"\(bookTitle)\" with bookId \(bookId), at index \(index)")
}
}
章节架子
import ShelfView
class SectionShelfController: UIViewController, SectionShelfViewDelegate {
var shelfView: SectionShelfView!
override func viewDidLoad() {
super.viewDidLoad()
let books = [
BookModel(bookCoverSource: "https://files.kerching.raywenderlich.com/covers/d5693015-46b6-44f8-bf7b-7a222b28d9fe.png", bookId: "0", bookTitle: "Realm: Building Modern Swift Apps with Realm"),
BookModel(bookCoverSource: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTYEkCQ_wu8HoGJzzs_gUH_FVusgI2RhntBKQ-WkmqnDJZnriwY6Q", bookId: "1", bookTitle: "iOS 10 by Tutorials: Learning the new iOS APIs with Swift 3")
]
let bookModelSectionArray = [BookModelSection(sectionName: "RAYWENDERLICH", sectionId: "0", sectionBooks: books)]
shelfView = SectionShelfView(frame: CGRect(x: 0, y: 0, width: 350, height: 500), bookModelSection: bookModelSectionArray, bookSource: SectionShelfView.BOOK_SOURCE_URL)
shelfView.delegate = self
self.view.addSubview(shelfView)
}
func onBookClicked(_ shelfView: SectionShelfView, section: Int, index: Int, sectionId: String, sectionTitle: String, bookId: String, bookTitle: String) {
print("I just clicked \"\(bookTitle)\" with bookId \(bookId), at index \(index). Section details --> section \(section), sectionId \(sectionId), sectionTitle \(sectionTitle)")
}
}
向 ShelfView 添加更多书籍
- 普通架子
addBooks(bookModel: [BookModel])
- 章节架子
addBooks(bookModelSection: [BookModelSection])
在 ShelfView 上重新加载书籍
- 普通架子
reloadBooks(bookModel: [BookModel])
- 章节架子
reloadBooks(bookModelSection: [BookModelSection])
从其他来源加载书籍封面
- iPhone/iPad 文档目录
let books = [
BookModel(bookCoverSource: "bookcover0.png", bookId: "0", bookTitle: "Book Title 0"),
BookModel(bookCoverSource: "bookcover1.png", bookId: "1", bookTitle: "Book Title 1")
]
shelfView = PlainShelfView(frame: CGRect(x: 0, y: 0, width: 350, height: 500), bookModel: books, bookSource: PlainShelfView.BOOK_SOURCE_DEVICE_DOCUMENTS)
- iPhone/iPad 图书馆目录
let books = [
BookModel(bookCoverSource: "bookcover0.png", bookId: "0", bookTitle: "Book Title 0"),
BookModel(bookCoverSource: "bookcover1.png", bookId: "1", bookTitle: "Book Title 1")
]
shelfView = PlainShelfView(frame: CGRect(x: 0, y: 0, width: 350, height: 500), bookModel: books, bookSource: PlainShelfView.BOOK_SOURCE_DEVICE_LIBRARY)
- iPhone/iPad 缓存目录
let books = [
BookModel(bookCoverSource: "bookcover0.png", bookId: "0", bookTitle: "Book Title 0"),
BookModel(bookCoverSource: "bookcover1.png", bookId: "1", bookTitle: "Book Title 1")
]
shelfView = PlainShelfView(frame: CGRect(x: 0, y: 0, width: 350, height: 500), bookModel: books, bookSource: PlainShelfView.BOOK_SOURCE_DEVICE_CACHE)
- 直接从您项目的源代码
let books = [
BookModel(bookCoverSource: "bookcover0.png", bookId: "0", bookTitle: "Book Title 0"),
BookModel(bookCoverSource: "bookcover1.png", bookId: "1", bookTitle: "Book Title 1")
]
shelfView = PlainShelfView(frame: CGRect(x: 0, y: 0, width: 350, height: 500), bookModel: books, bookSource: PlainShelfView.BOOK_SOURCE_RAW)
许可
ShelfView
采用MIT许可。有关更多信息,请参阅LICENSE文件。
作者
Adeyinka Adediji ([email protected])