iOS custom view to display books on shelf
(Android version is available here)
is available through CocoaPods. To install it, simply add the following line to your Podfile:
- ShelfView
- pod 'ShelfView'
Because of book covers whose URLs are
, update your
- http
as follows:
- info.plist
to the list
- App Transport Security Settings
to the security settings added above; set it to
- Allow Arbitrary Loads
.
- YES
- import ShelfView
-
- class ViewController: UIViewController, ShelfViewDelegate {
-
- var shelfView:ShelfView!
- var bookModel = [BookModel] ()
-
- override func viewDidLoad() {
-
- super.viewDidLoad()
-
- shelfView = ShelfView(frame: CGRect(x: 0, y: 0, width: 375, height: 647))
- bookModel.append(
- BookModel.init(bookCoverSource: "http://www.aidanf.net/images/learn-swift/cover-web.png",
- bookId: "0",
- bookTitle: "Learn Swift"))
-
- shelfView.loadData(bookModel: bookModel,
- bookSource: ShelfView.BOOK_SOURCE_URL)
-
- shelfView.delegate = self
- self.view.addSubview(shelfView)
- }
-
- func onBookClicked(_ shelfView: ShelfView, position: Int, bookId: String, bookTitle: String) {
- print(
- "I just clicked \(bookTitle) with bookId \(bookId) @ position \(position)"
- )
- }
-
- }
Updating ShelfView data
- bookModel.append(
- BookModel.init(bookCoverSource: "https://www.packtpub.com/sites/default/files/9781785288197.png",
- bookId: "1",
- bookTitle: "Learning iOS UI Development"))
- shelfView.updateData(bookModel: self.bookModel)
Resizing ShelfView
- shelfView.resize(width: 647, height: 370, bookModel: self.bookModel)
Loading book covers from other sources
- bookModel.append(
- BookModel.init(bookCoverSource: "bookCover.png",
- bookId: "0",
- bookTitle: "Learn Swift"))
-
- shelfView.loadData(bookModel: bookModel,
- bookSource: ShelfView.BOOK_SOURCE_DEVICE_DOCUMENTS)
- bookModel.append(
- BookModel.init(bookCoverSource: "bookCover.png",
- bookId: "0",
- bookTitle: "Learn Swift"))
-
- shelfView.loadData(bookModel: bookModel,
- bookSource: ShelfView.BOOK_SOURCE_DEVICE_LIBRARY)
- bookModel.append(
- BookModel.init(bookCoverSource: "bookCover.png",
- bookId: "0",
- bookTitle: "Learn Swift"))
-
- shelfView.loadData(bookModel: bookModel,
- bookSource: ShelfView.BOOK_SOURCE_DEVICE_CACHE)
- bookModel.append(
- BookModel.init(bookCoverSource: "bookCover.png",
- bookId: "0",
- bookTitle: "Learn Swift"))
-
- shelfView.loadData(bookModel: bookModel,
- bookSource: ShelfView.BOOK_SOURCE_RAW)
is available under the MIT license. See the LICENSE file for more info.
- ShelfView
Adeyinka Adediji (tdscientist@gmail.com)
tdscientist@gmail.com
来源: https://juejin.im/entry/59daf0a15188250eb609b347